从data.table

时间:2016-01-08 10:10:55

标签: r data.table

我想了解data.table的做事方式。我试图列出一些从data.table中提取子集的不同方法。我的目的是学习语法。

DT <- data.table(x1=c("b","c", "a", "b", "a", "b"), x2a=as.character(1:6),m1=seq(10,60,by=10),m2=1:6)

#### Method 1
DT[x1 == 'a'& x2a == '3',1:3, with=FALSE]
   x1 x2a m1
1:  a   3 30

#### Method 2
subset(DT,x1 == 'a' & x2a == '3',select = 1:3)
   x1 x2a m1
1:  a   3 30

#### Method 3
setkey(DT,x1,x2a)
DT[.('a','3')]
   x1 x2a m1 m2
1:  a   3 30  3

#### Method 4 - No keys set
DT[,.(x1='a',x2a='3')] # how do I get all the columns?
   x1 x2a
1:  a   3
DT[,.(x1='a',x2a='3'),.SD,.SDcols=1:3] #error message
DT[,.(x1='a',x2a='3'),lapply(.SD),.SDcols=1:3] # what function should I give just to get the rows?
Error in match.fun(FUN) : argument "FUN" is missing, with no default

#### Method 5 - No keys set
DT[.('a','3'),by=.(x1,x2a)] # Should this not be the same as key setting?
DT[.('a','3'),by=.('x1','x2a')] # Should there be quotes on x1 and x2a? When?
DT[.('a','3'),keyby=.('x1','x2a')] # When and where should keyby be used? Nice to see an example

方法1到3工作正常。但我想帮助方法4和5。

我想从方法4和5得到相同的结果,从1到3.我如何使用方法4并使用.SD&amp; .SDcols?

0 个答案:

没有答案
相关问题