dcast 中的 value.var 可以是一个列表还是有多个值变量? [英] can the value.var in dcast be a list or have multiple value variables?
问题描述
在 dcast.data.table
的帮助文件中,有一条注释说明已实现了一项新功能:dcast.data.table 允许 value.var 列为列表类型"
In the help files for dcast.data.table
, there is a note stating that a new feature has been implemented: "dcast.data.table allows value.var column to be of type list"
我认为这意味着一个列表中可以有多个值变量,即采用以下格式:
I take this to mean that one can have multiple value variables within a list, i.e. in this format:
dcast.data.table(dt, x1~x2, value.var=list('var1','var2','var3'))
但是我们得到一个错误:'value.var' must be a character vector of length 1.
But we get an error: 'value.var' must be a character vector of length 1.
是否有这样的功能,如果没有,还有其他单行替代方案吗?
Is there such a feature, and if not, what would be other one-liner alternatives?
回复以下评论
在某些情况下,您希望将多个变量视为 value.var
.例如,假设 x2 包含 3 个不同的周,并且您有 2 个值变量,例如盐和糖的消耗量,并且您希望将这些变量转换为不同的周.当然,您可以将 2 个值变量融合"到单个列中,但是当您可以像 reshape
那样在一个函数中完成时,为什么要使用两个函数来做某事呢?
There are situations where you have multiple variables that you want to treat as the value.var
. Imagine for example that x2 consists of 3 different weeks, and you have 2 value variables such as salt and sugar consumption and you want to cast those variables across the different weeks. Sure, you can 'melt' the 2 value variables into a single column, but why do something using two functions, when you can do it in one function like reshape
does?
(注意:我还注意到 reshape
不能像 dcast
那样将多个变量视为时间变量.)
(Note: I've also noticed that reshape
cannot treat multiple variables as the time variable as dcast
does.)
所以我的意思是我不明白为什么这些函数不允许灵活地在 value.var
或 time.var
就像我们允许 id.var
有多个变量一样.
So my point is that I don't understand why these functions don't allow for the flexibility to include multiple variables within the value.var
or the time.var
just as we allow for multiple variables for the id.var
.
推荐答案
从 data.table v1.9.6 开始,我们可以同时转换多个 value.var
列(也可以使用多个聚合函数在fun.aggregate
).请参阅 ?dcast
和 使用 data.tables 进行高效整形 小插图了解更多.
From v1.9.6 of data.table, we can cast multiple value.var
columns simultaneously (and also use multiple aggregation functions in fun.aggregate
). Please see ?dcast
and the Efficient reshaping using data.tables vignette for more.
以下是我们如何使用 dcast
:
Here's how we could use dcast
:
dcast(setDT(mydf), x1 ~ x2, value.var=c("salt", "sugar"))
# x1 salt_1 salt_2 salt_3 sugar_1 sugar_2 sugar_3
# 1: 1 3 4 6 1 2 2
# 2: 2 10 3 9 5 3 6
# 3: 3 10 7 7 4 6 7
这篇关于dcast 中的 value.var 可以是一个列表还是有多个值变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!