我想結合使用j的.SD
lapply的結果與j.
的其他輸出列我怎樣才能在同一個數據表中做到這一點? 到目前爲止,我創建兩個數據表(example_summary1, example_summary2)
併合並他們,但應該有更好的方法? 也許我不完全理解的.SD/.SDcols.
R數據表將lapply與其他j參數結合起來
example <-data.table(id=rep(1:5,3),numbers=rep(1:5,3),sample1=sample(20,15,repla ce=TRUE),sample2=sample(20,15,replace=100))
id numbers sample1 sample2
1: 1 1 17 18
2: 2 2 8 1
3: 3 3 17 12
4: 4 4 15 2
5: 5 5 14 18
6: 1 1 11 14
7: 2 2 12 12
8: 3 3 11 7
9: 4 4 16 13
10: 5 5 17 1
11: 1 1 10 3
12: 2 2 14 15
13: 3 3 13 3
14: 4 4 17 6
15: 5 5 1 5
example_summary1<-example[,lapply(.SD,mean),by=id,.SDcols=c("sample1","sample2")]
> example_summary1
id sample1 sample2
1: 1 12.66667 11.666667
2: 2 11.33333 9.333333
3: 3 13.66667 7.333333
4: 4 16.00000 7.000000
5: 5 10.66667 8.000000
example_summary2<-example[,.(example.sum=sum(numbers)),id]
> example_summary2
id example.sum
1: 1 3
2: 2 6
3: 3 9
4: 4 12
5: 5 15
你有多少列? – Sotos
在我的真實數據中,我有大約42列,我想用'.SDcols'和一些其他列來處理我想包含到j輸出列表中的列。 – Cracker
這些列會干擾'SDcols'之外的其他列。我不確定這是可能的,還是我只能使用'SDcols'? – Cracker