使用以下内容分配多个列:= data.table,group

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


f <- function(x) {c("hi", "hello")}

x <- data.table(id = 1:10)

我想做这样的事情(当然这种语法不正确):


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

为了扩展它,我可能有很多列的名称存储在一个变量(比如说col_names)中,我想这样做:


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

做这样的事的正确方法是什么?


皈依舞
浏览 532回答 3
3回答

慕尼黑的夜晚无繁华

这现在适用于R-Forge的v1.8.3。谢谢你突出它!x <- data.table(a = 1:3, b = 1:6)&nbsp;f <- function(x) {list("hi", "hello")}&nbsp;x[ , c("col1", "col2") := f(), by = a][]#&nbsp; &nbsp; a b col1&nbsp; col2# 1: 1 1&nbsp; &nbsp;hi hello# 2: 2 2&nbsp; &nbsp;hi hello# 3: 3 3&nbsp; &nbsp;hi hello# 4: 1 4&nbsp; &nbsp;hi hello# 5: 2 5&nbsp; &nbsp;hi hello# 6: 3 6&nbsp; &nbsp;hi hellox[ , c("mean", "sum") := list(mean(b), sum(b)), by = a][]#&nbsp; &nbsp; a b col1&nbsp; col2 mean sum# 1: 1 1&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5# 2: 2 2&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7# 3: 3 3&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9# 4: 1 4&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5# 5: 2 5&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7# 6: 3 6&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp;mynames = c("Name1", "Longer%")x[ , (mynames) := list(mean(b) * 4, sum(b) * 3), by = a]#&nbsp; &nbsp; &nbsp;a b col1&nbsp; col2 mean sum Name1 Longer%# 1: 1 1&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5&nbsp; &nbsp; 10&nbsp; &nbsp; &nbsp; 15# 2: 2 2&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7&nbsp; &nbsp; 14&nbsp; &nbsp; &nbsp; 21# 3: 3 3&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp; &nbsp; 18&nbsp; &nbsp; &nbsp; 27# 4: 1 4&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5&nbsp; &nbsp; 10&nbsp; &nbsp; &nbsp; 15# 5: 2 5&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7&nbsp; &nbsp; 14&nbsp; &nbsp; &nbsp; 21# 6: 3 6&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp; &nbsp; 18&nbsp; &nbsp; &nbsp; 27x[ , mynames := list(mean(b) * 4, sum(b) * 3), by = a, with = FALSE][] # same#&nbsp; &nbsp; a b col1&nbsp; col2 mean sum Name1 Longer%# 1: 1 1&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5&nbsp; &nbsp; 10&nbsp; &nbsp; &nbsp; 15# 2: 2 2&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7&nbsp; &nbsp; 14&nbsp; &nbsp; &nbsp; 21# 3: 3 3&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp; &nbsp; 18&nbsp; &nbsp; &nbsp; 27# 4: 1 4&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5&nbsp; &nbsp; 10&nbsp; &nbsp; &nbsp; 15# 5: 2 5&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7&nbsp; &nbsp; 14&nbsp; &nbsp; &nbsp; 21# 6: 3 6&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp; &nbsp; 18&nbsp; &nbsp; &nbsp; 27x[ , get("mynames") := list(mean(b) * 4, sum(b) * 3), by = a][]&nbsp; # same#&nbsp; &nbsp; a b col1&nbsp; col2 mean sum Name1 Longer%# 1: 1 1&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5&nbsp; &nbsp; 10&nbsp; &nbsp; &nbsp; 15# 2: 2 2&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7&nbsp; &nbsp; 14&nbsp; &nbsp; &nbsp; 21# 3: 3 3&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp; &nbsp; 18&nbsp; &nbsp; &nbsp; 27# 4: 1 4&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5&nbsp; &nbsp; 10&nbsp; &nbsp; &nbsp; 15# 5: 2 5&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7&nbsp; &nbsp; 14&nbsp; &nbsp; &nbsp; 21# 6: 3 6&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp; &nbsp; 18&nbsp; &nbsp; &nbsp; 27x[ , eval(mynames) := list(mean(b) * 4, sum(b) * 3), by = a][]&nbsp; &nbsp;# same#&nbsp; &nbsp; a b col1&nbsp; col2 mean sum Name1 Longer%# 1: 1 1&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5&nbsp; &nbsp; 10&nbsp; &nbsp; &nbsp; 15# 2: 2 2&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7&nbsp; &nbsp; 14&nbsp; &nbsp; &nbsp; 21# 3: 3 3&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp; &nbsp; 18&nbsp; &nbsp; &nbsp; 27# 4: 1 4&nbsp; &nbsp;hi hello&nbsp; 2.5&nbsp; &nbsp;5&nbsp; &nbsp; 10&nbsp; &nbsp; &nbsp; 15# 5: 2 5&nbsp; &nbsp;hi hello&nbsp; 3.5&nbsp; &nbsp;7&nbsp; &nbsp; 14&nbsp; &nbsp; &nbsp; 21# 6: 3 6&nbsp; &nbsp;hi hello&nbsp; 4.5&nbsp; &nbsp;9&nbsp; &nbsp; 18&nbsp; &nbsp; &nbsp; 27

慕丝7291255

以下简写符号可能有用。所有的功劳都归功于Andrew Brooks,特别是这篇文章。dt[,`:=`(avg=mean(mpg), med=median(mpg), min=min(mpg)), by=cyl]

温温酱

我应该如何修改以下行,以便从dim输出中为每个objectName获取两列,而不是一列包含两行?data.table(objectName=ls())[,c("rows","cols"):=dim(get(objectName)),by=objectName](我正在使用data.table1.8.11)
打开App,查看更多内容
随时随地看视频慕课网APP