Maybe look at reshape2? e.g. https://seananderson.ca/2013/10/19/reshape/

There is a very capable base R reshape function, but I find it quite 
challenging to apply.

Note that you normally want to retain one or more columns to identify each row 
in your result... but I am not sure I see anything suitable in your example 
data. You may have removed it, or it may have been implied and you may need to 
create something that will do the job.

e.g. https://www.studytonight.com/dbms/database-key.php

On June 29, 2019 11:27:40 AM PDT, Janet Choate <jsc....@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

-- 
Sent from my phone. Please excuse my brevity.

______________________________________________
R-help@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.

Reply via email to