Output multiple objects from function R -
i'm not sure if question exists or not. after looking around internet, can't find relates specific problem.
so, in r programming, mass output objects (specifically time series objects) data frame splitting it. before this, have been pasting duplicate concatenation commands each user , feels extremely tedious process, wondering if build function split data frame variable, apply ts() function, label object created, , output objects created.
now ideally, first thing thought of using command , apply function each split. example, below code tried (note, small example, real data frame larger this).
#test df stackoverflow df<-data.frame(user=c(rep(1,5),rep(2,5),rep(3,5),rep(4,5))) df$values<-c(10,20,30,40,9,19,29,39,11,21,31,41,12,22,32,42,8,18,34,44) #playing around command dd<-by(df,df$user,fun=function(x){ time = ts(x$values,freqency=2), label = x$user[1], label<-time, return(label) })
the error says there's error squiggly bracket. i've looked @ ddply in 'plyr' package, couldn't working either.
any advice/help/comments appreciated, thanks.
if understood correctly want split data.frame variable , transform each portion time series object.
you can use plyr
, result list (if number of data per user not equal)
require(plyr) dlply(df, .(user), function(df) ts(df$value, frequency = 12))
you can still use by
if want too
by(df$value, df$user, function(x) ts(x, frequency = 12)) ## df$user: 1 ## jan feb mar apr may ## 1 10 20 30 40 9 ## ------------------------------------------------ ## df$user: 2 ## jan feb mar apr may ## 1 19 29 39 11 21 ## ------------------------------------------------ ## df$user: 3 ## jan feb mar apr may ## 1 31 41 12 22 32 ## ------------------------------------------------ ## df$user: 4 ## jan feb mar apr may ## 1 42 8 18 34 44