使用:=在data.table中按组分配多个列 [英] Assign multiple columns using := in data.table, by group

查看:152
本文介绍了使用:=在data.table中按组分配多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 data.table 为多个列分配最佳方式是什么?例如:

What is the best way to assign to multiple columns using data.table? For example:

f <- function(x) {c("hi", "hello")}
x <- data.table(id=1:10)

我想做一些事(当然这个语法不正确):

I would like to do something liket his (of course this syntax is incorrect):

x[,(col1, col2):=f(), by="id]

并且为了扩展,我有一个变量名称的列c $ c> column_names ),我想这样做:

And to extend that I make have many columns with names in a variable (say column_names) and I would like to do:

x[,col_names:=another_f(), by="id", with=FALSE]

推荐答案

现在可以在R-Forge的v1.8.3中使用。

This now works in v1.8.3 on R-Forge. Thanks for highlighting it!

x <- data.table(a=1:3,b=1:6) 
f <- function(x) {list("hi", "hello")} 
x[,c("col1","col2"):=f(), by=a][]
   a b col1  col2
1: 1 1   hi hello
2: 2 2   hi hello
3: 3 3   hi hello
4: 1 4   hi hello
5: 2 5   hi hello
6: 3 6   hi hello
x[,c("mean","sum"):=list(mean(b),sum(b)),by=a][]
   a b col1  col2 mean sum
1: 1 1   hi hello  2.5   5
2: 2 2   hi hello  3.5   7
3: 3 3   hi hello  4.5   9
4: 1 4   hi hello  2.5   5
5: 2 5   hi hello  3.5   7
6: 3 6   hi hello  4.5   9 

mynames = c("Name1","Longer%")
x[, (mynames) := list(mean(b)*4,sum(b)*3), by = a]
    a b col1  col2 mean sum Name1 Longer%
1: 1 1   hi hello  2.5   5    10      15
2: 2 2   hi hello  3.5   7    14      21
3: 3 3   hi hello  4.5   9    18      27
4: 1 4   hi hello  2.5   5    10      15
5: 2 5   hi hello  3.5   7    14      21
6: 3 6   hi hello  4.5   9    18      27


x[,mynames:=list(mean(b)*4,sum(b)*3),by=a,with=FALSE][] # same
   a b col1  col2 mean sum Name1 Longer%
1: 1 1   hi hello  2.5   5    10      15
2: 2 2   hi hello  3.5   7    14      21
3: 3 3   hi hello  4.5   9    18      27
4: 1 4   hi hello  2.5   5    10      15
5: 2 5   hi hello  3.5   7    14      21
6: 3 6   hi hello  4.5   9    18      27

x[,get("mynames"):=list(mean(b)*4,sum(b)*3),by=a][]  # same
   a b col1  col2 mean sum Name1 Longer%
1: 1 1   hi hello  2.5   5    10      15
2: 2 2   hi hello  3.5   7    14      21
3: 3 3   hi hello  4.5   9    18      27
4: 1 4   hi hello  2.5   5    10      15
5: 2 5   hi hello  3.5   7    14      21
6: 3 6   hi hello  4.5   9    18      27

x[,eval(mynames):=list(mean(b)*4,sum(b)*3),by=a][]   # same
   a b col1  col2 mean sum Name1 Longer%
1: 1 1   hi hello  2.5   5    10      15
2: 2 2   hi hello  3.5   7    14      21
3: 3 3   hi hello  4.5   9    18      27
4: 1 4   hi hello  2.5   5    10      15
5: 2 5   hi hello  3.5   7    14      21
6: 3 6   hi hello  4.5   9    18      27

这篇关于使用:=在data.table中按组分配多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆