R 将矢量输出转换为data.table中的列?

R 将矢量输出转换为data.table中的列?,r,data.table,R,Data.table,我通过将函数应用于data.table的某些子集来生成输出。我使用的函数如下: data[, foo(args), by=list(Year, Month)] Year Month V1 1: 1983 2 9.734669e-06 2: 1983 2 9.165665e-06 3: 1983 2 2.097477e-05 4: 1983 2 3.803727e-05 My functionfoo始终返回

我通过将函数应用于data.table的某些子集来生成输出。我使用的函数如下:

data[, foo(args), by=list(Year, Month)]
      Year Month           V1
   1: 1983     2 9.734669e-06
   2: 1983     2 9.165665e-06
   3: 1983     2 2.097477e-05
   4: 1983     2 3.803727e-05
My function
foo
始终返回长度为
n
的向量。我得到如下输出:

data[, foo(args), by=list(Year, Month)]
      Year Month           V1
   1: 1983     2 9.734669e-06
   2: 1983     2 9.165665e-06
   3: 1983     2 2.097477e-05
   4: 1983     2 3.803727e-05
但是我想要像这样的东西

      Year Month           V1           V2           V3           V4 ...
   1: 1983     2 9.734669e-06 9.165665e-06 2.097477e-05 3.803727e-05 ...
我甚至试过使用
list(foo(args))
,但都没有用。或者输出的格式应该是
foo$V1,foo$V2…

试试看

data[, as.list(foo(args)), by=list(Year, Month)] 
或者更改
foo
以返回
列表
,而不是
向量