我有一个数据框,我需要转动,但数据框有重复的标识符,所以spread函数给出一个错误Error: Duplicate identifiers for rows (5, 6)
Dimension = c("A","A","B","B","A","A")
Date = c("Mon","Tue","Mon","Wed","Fri","Fri")
Metric = c(23,25,7,9,7,8)
df = data.frame(Dimension,Date,Metric)
df
Dimension Date Metric
1 A Mon 23
2 A Tue 25
3 B Mon 7
4 B Wed 9
5 A Fri 7
6 A Fri 8
library(tidyr)
df1 = spread(df, Date, Metric, fill = " ")
Error: Duplicate identifiers for rows (5, 6)
Run Code Online (Sandbox Code Playgroud)
然后我合并了行并粘贴了Metric:
dfa = aggregate(df[3], df[-3],
FUN = function(X) paste(unique(X), collapse=", ")) …Run Code Online (Sandbox Code Playgroud) 当你将变量传递给函数时,lua有扩展运算符吗?
例如,我有一个数组a,我想将其传递给另一个函数,例如string.format。如果我这样做的话string.format(a)我就会得到
bad argument #1 to 'format' (string expected, got table)
Run Code Online (Sandbox Code Playgroud)
我尝试过local f, e = pcall(string.format, t)但没有运气。
下面出现错误 TS2556,如何修复?
class Test {
constructor(x: number) {}
}
class Test2 extends Test {
constructor(...args) {
super(...args); // TS2556
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用 jsdoc 和 tsc 进行类型检查:
class Test {
constructor(x: number) {}
}
class Test2 extends Test {
constructor(...args) {
super(...args); // TS2556
}
}
Run Code Online (Sandbox Code Playgroud) 我不知道我是否正在使用此权限,但是我有以下sass/compass代码:
+text-shadow(red 0 3px 0 3px)
Run Code Online (Sandbox Code Playgroud)
产生以下内容css:
text-shadow: red 0 3px 3px, red 0 3px 0 3px;
text-shadow: red 0 3px 0 3px, red 0 3px 0 3px;
Run Code Online (Sandbox Code Playgroud)
这在Chrome / Safari / Firefox / Opera中均不起作用。
这是声明中的内容,还是此spread功能实际上已从规格中删除?
鉴于示例数据,我想要spread一key-value对子集.在这种情况下,它只是一对.然而,在其他情况下,子集spread是多于一对.
library(tidyr)
# dummy data
> df1 <- data.frame(e = c(1, 1, 1, 1),
n = c("a", "b", "c", "d") ,
s = c(1, 2, 5, 7))
> df1
e n s
1 1 a 1
2 1 b 2
3 1 c 5
4 1 d 7
Run Code Online (Sandbox Code Playgroud)
所有键值对的经典传播:
> df1 %>% spread(n,s)
e a b c d
1 1 1 2 5 7
Run Code Online (Sandbox Code Playgroud)
期望的输出,仅传播 n=c
e c n s
1 1 5 a …Run Code Online (Sandbox Code Playgroud) 我正在尝试更新我的 redux 状态的一部分,其中包含嵌套在另一个对象中的两个对象。通常,当不可变地更新 javascript 对象时,我会使用扩展运算符,然后按如下方式定义任何更改:
state = {...state, property1: newvalue}
Run Code Online (Sandbox Code Playgroud)
但是,当我有嵌套对象时,我不确定如何使用扩展运算符。这是相关的代码和我的尝试:
const squadDatabase = {currentSquad: {
0: null,
1: null,
2:null
}, newAdditions: null}
export default (state=initial_squad, action)=>{
switch(action.type){
case ADD_PLAYER_TO_SQUAD:
return {...state, currentSquad[action.payload.currentSquadMemberId]:action.payload.newSquadAdditionId, newAdditions: action.payload.newSquadAdditionId}
default:
return initial_squad
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何使用扩展运算符或其他方式不可变地更新嵌套的 javascript 对象?
因此,目前我正在尝试弄清楚如何从 MovieLense 构建电影推荐系统(https://grouplens.org/datasets/movielens/100k/)。我阅读了教程中的一些说明。
library(dplyr)
library(recommenderlab)
library(magrittr)
data <- read.table("u.data", header = F, stringsAsFactors = T)
head(data)
V1 V2 V3 V4
1 196 242 3 881250949
2 186 302 3 891717742
3 22 377 1 878887116
4 244 51 2 880606923
5 166 346 1 886397596
6 298 474 4 884182806
Run Code Online (Sandbox Code Playgroud)
说明:V1是 userid,V2是 itemid,V3是 rating
现在我需要将格式记录到 ratingMatrix,结果将是这样的:
1 2 3 4 5 6 7 8 9 10
1 5 3 4 3 3 …Run Code Online (Sandbox Code Playgroud) 我有以下对象
let car = { year: 2004, type: {name: "BMW"} }
Run Code Online (Sandbox Code Playgroud)
现在我想向内部对象“类型”添加一个属性。我想用扩展运算符来实现这一点,因为我需要一个新对象,因为现有对象是一个不可变的状态对象。结果应该是:
{ year: 2004, type: {name: "BMW", modell: "3er"}}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有一个函数应该返回某种类型,但使用扩展运算符导致它分配拼写错误的键。
interface State {
fieldA: number,
fieldB: string
}
const reducer: (state: State, action: {payload: string}) => State = (state, action) => {
// please note that those variables are not desired
const tmp = { ...state, feildB: action.payload }; // No compile time error, :(
// This is too verbose... but works
const tmp2 = Object.assign<State, Partial<State>>(state, {feildB: action.payload}) // ERROR - this is what I need
return tmp
}
const t = reducer({fieldA: 1, fieldB: 'OK'}, {payload: 'Misspelled'}) …Run Code Online (Sandbox Code Playgroud) 我有以下定义数组的代码,然后带有生成器的迭代器对象从该数组中生成值,并使用扩展运算符输出每个值:
\nconst arr = ['0', '1', '4', 'a', '9'];\nconst my_obj = {\n [Symbol.iterator]: function*() {\n for(let index of arr) {\n yield `${index}`;\n }\n }\n};\n\nconst all = [...my_obj] \n\nconsole.log(...my_obj)\nRun Code Online (Sandbox Code Playgroud)\n结果是:
\n0\n1\n4\na\n9\nRun Code Online (Sandbox Code Playgroud)\n我不明白\xe2\x80\x99t 的是扩展运算符变量\xe2\x80\x9c...my_obj\xe2\x80\x9d 如何获取数组的值如果\xe2\x80\x9cmy_obj\xe2\x80 \x9d 是一个对象,而不是数组。据我了解: \xe2\x80\x9cmy_obj\xe2\x80\x9d 正在接收一个对象,如果应用扩展运算符,它应该得到 \xe2\x80\x9ckey:value\xe2\x80\x9d。
\n有人能解释一下它是如何获得这些值的吗?
\n这是代码
funtion App(){
const [Cards, setCards] = useState([
{id: 0 , name: 'Harry Potter'},
{id: 1 , name: 'Hermonie Granger'},
{id: 2 , name: 'Ron Weasly'},])
const shuffle = (arr)=>{
// just shuffle the arr then return it.
}
return (
<div className="App">
<div>{Cards[0].name}</div>
<button onClick={() => {
setCards([...shuffle(Cards)])
console.log(Cards)
}}>Testing</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
为什么这个有效setCards([...shuffle(Cards)])
,而不是这个setCards(shuffle(Cards))。
即使我不使用展开运算符,它也会洗牌(请参阅控制台),但不会在页面中显示它。
:) ;) ....
spread ×11
javascript ×3
r ×3
tidyr ×3
typescript ×3
dplyr ×2
reactjs ×2
aggregate ×1
arrays ×1
compass-sass ×1
css ×1
css3 ×1
generator ×1
iterator ×1
jsdoc ×1
lua ×1
object ×1
react-hooks ×1
redux ×1
super ×1