Rotate vector output into columns in data.table?

I generate output by applying a function to some subsets of data.table. I use the function as follows:

data[, foo(args), by=list(Year, Month)] 

My foo function always returns a vector of length n . I get the output as follows:

  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 

But I would like something like

  Year Month V1 V2 V3 V4 ... 1: 1983 2 9.734669e-06 9.165665e-06 2.097477e-05 3.803727e-05 ... 

I even tried using list(foo(args)) , this did not help. Or should the output be in the form foo$V1, foo$V2 ... ?

+4
source share
1 answer

Try

 data[, as.list(foo(args)), by=list(Year, Month)] 

or change foo to return list , not vector .

+7
source

Source: https://habr.com/ru/post/1438927/


All Articles