[R] turning a list of objects into columns

Janet Choate j@c@eco @end|ng |rom gm@||@com
Sat Jun 29 20:27:40 CEST 2019


Hi,
I have a data frame that looks something like this (but much longer):
df
scen streamflow    trans     evap      psn
1   0.019234 1.658967 0.002883 0.002391
1   0.019027 1.661192 0.002844 0.003142
2   0.018821 1.695623 0.003192 0.002167
2   0.018619 1.503481 0.002536 0.003059
3   0.018425 0.000008 1.880355 0.002592
3   0.018369 0.100551 2.225793 0.006642

i want to end up with something like this (for each variable - streamflow,
trans, evap, psn). using the variable trans here as an example:
trans1        trans2      trans3
1.658967  1.695623  0.000008
1.661192  1.503481  0.100551

so that each variable (streamflow, trans, evap, psn) is in a separate
column based on the scen #.

i used split which created a list for each scen #:
test = split(df[,,], df$scen)
as well as
test = as.data.frame(split(df[,,], df$scen)

which did separate out each scen instance, but in lists, i.e.:
 $`1`
   [1] 1.658967 1.661192
$`2`
   [1] 1.695623 1.503481
$`3`
   [1] 0.000008 0.100551

if i use as.data.frame, i.e.:
test2 = as.data.frame(test)

it seems like it puts it into columns, i.e.:
        X1       X2       X3
1 1.658967  1.695623  0.000008
2 1.661192  1.503481  0.100551

however, if i look at one of the variables, it still presents as a list,
even though the class is numeric:
head(tmp2$X1)
[1] 1.658967 1.661192

can anyone tell me how to get this into the format i want - where each
variable for each scen # is in it's own column in a data frame?
thank you,
Janet

-- 
Tague Team Lab Manager
1005 Bren Hall
UCSB, Santa Barbara, CA.

	[[alternative HTML version deleted]]



More information about the R-help mailing list