5

I am generating output by applying function to some subsets of data.table. I am using function like this:

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

My function foo return always a vector of length n. I am getting an output like this:

      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)), it didn't help. Or should the output be in form foo$V1, foo$V2 ...?

1 Answer 1

9

Try

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

or change foo to return a list rather than a vector.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.