相关疑难解决方法(0)

在`data.table`中使用动态列名

我想计算data.table中每个列的平均值,按另一列分组.我的问题类似于关于SO的另外两个问题(一个两个),但我不能将这些问题应用于我的问题.

这是一个例子:

library(data.table)
dtb <- fread(input = "condition,var1,var2,var3
      one,100,1000,10000
      one,101,1001,10001
      one,102,1002,10002
      two,103,1003,10003
      two,104,1004,10004
      two,105,1005,10005
      three,106,1006,10006
      three,107,1007,10007
      three,108,1008,10008
      four,109,1009,10009
      four,110,1010,10010")

dtb
#    condition var1 var2  var3
# 1:       one  100 1000 10000
# 2:       one  101 1001 10001
# 3:       one  102 1002 10002
# 4:       two  103 1003 10003
# 5:       two  104 1004 10004
# 6:       two  105 1005 10005
# 7:     three  106 1006 10006
# 8:     three  107 1007 10007
# 9:     three …
Run Code Online (Sandbox Code Playgroud)

r data.table

32
推荐指数
1
解决办法
2万
查看次数

在R data.table中连接列名的向量

我想要添加一个列,data.table它是几个其他列的串联,其中的名称我存储在向量中cols.根据/sf/answers/1517778181/我试过do.call+ paste但无法使其正常工作.这是我尝试过的:

# Using mtcars as example, e.g. first record should be "110 21 6"
dt <- data.table(mtcars)
cols <- c("hp", "mpg", "cyl")
# Works old-fashioned way
dt[, slice.verify := paste(hp, mpg, cyl)]
# Raw do.call+paste fails with message:
# Error in do.call(paste, cols): second argument must be a list
dt[, slice := do.call(paste, cols)]
# Making cols a list makes the column "hpmpgcyl" for each row
dt[, slice := do.call(paste, …
Run Code Online (Sandbox Code Playgroud)

r data.table

3
推荐指数
1
解决办法
845
查看次数

标签 统计

data.table ×2

r ×2