[R] turning a list of objects into columns

Eric Berger er|cjberger @end|ng |rom gm@||@com
Sat Jun 29 20:58:13 CEST 2019


Hi Janet,
I am not sure what your problem is. It seems that the code you wrote is
doing what you want.
The command 'head(tmp2$X1)' is outputting a numeric vector, not a list.

Eric


On Sat, Jun 29, 2019 at 9:28 PM Janet Choate <jsc.eco using gmail.com> wrote:

> 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]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list