[R] splitting a column of data into multiple columns

Glenn Doherty g|ennrdoherty @end|ng |rom gm@||@com
Sun Jun 30 20:47:29 CEST 2019


A very elegant solution Jim. Here is a tidyverse solution. Janet, this is a
different sample set (the same you sent in another post a couple days ago),
but it will work with this data frame as well.

### RECREATING YOUR DATA FRAME
scen<-c("1","1","2","2","3","3")
streamflow<-c("0.019234","0.019027","0.018821","0.018619","0.018425","0.018369")
trans<-c("1.658967","1.661192","1.695623","1.503481","0.000008","0.100551")
evap<-c("0.002883","0.002844","0.003192","0.002536","1.880355","2.225793")
psn<-c("0.002391","0.003142","0.002167","0.003059","0.002592","0.006642")
df<-as.data.frame(cbind(scen=as.numeric(scen),
                        streamflow=as.numeric(streamflow),
                        trans=as.numeric(trans),
                        evap=as.numeric(evap),
                        psn=as.numeric(psn)),
                  stringsAsFactors = FALSE)

###Data Manipulation
library(tidyverse)
df2<-gather(df, variable, value, -scen)%>%
  unite(scen_variable, scen, variable)%>%
  mutate(Row=sequence(rle(scen_variable)$lengths))%>%
  spread(scen_variable, value)%>%
  dplyr::select(-Row)

On Fri, Jun 28, 2019 at 2:46 PM Jim Lemon <drjimlemon using gmail.com> wrote:

> Hi Janet,
> This might help:
>
> jcdf<-read.table(text="scen trans evap flow
> 1       1.1    0.1    0.09
> 1       1.2    0.2    0.10
> 1       1.3    0.3    0.20
> 2       2.1    0.1    0.09
> 2       2.2    0.2    0.10
> 2       2.3    0.3    0.20
> 3       3.1    0.1    0.09
> 3       3.2    0.2    0.10
> 3       3.3    0.3    0.20",
> header=TRUE)
> library(prettyR)
> stretch_df(jcdf,"scen",c("trans","evap","flow"))[,2:10]
>
> Jim
>
>
> On Sat, Jun 29, 2019 at 1:42 AM Janet Choate <jsc.eco using gmail.com> wrote:
> >
> > Hello R community,
> > I have a data frame that has multiple observations in a single column
> that
> > I would like to break into multiple columns.
> > The data looks something like this:
> >
> > scen trans evap flow
> > 1       1.1    0.1    0.09
> > 1       1.2    0.2    0.10
> > 1       1.3    0.3    0.20
> > 2       2.1    0.1    0.09
> > 2       2.2    0.2    0.10
> > 2       2.3    0.3    0.20
> > 3       3.1    0.1    0.09
> > 3       3.2    0.2    0.10
> > 3       3.3    0.3    0.20
> >
> > the column scen runs from 1 through 500, and each scen # contains 1461
> rows
> > - i.e. there are 1461 observations for scen1, 1461 observations for
> scen2,
> > etc...
> > i want to split the trans, evap, and flow columns out by scen #, so that
> i
> > end up with something like:
> >
> > trans1   trans2    trans3
> > 1.1         2.1        1.1
> > 1.2         2.2        3.2
> > 1.3         2.3        3.3
> >
> > and same for the other variables.
> > thought i could use the separate command to do this, but either not the
> > right approach or i am not executing it properly.
> > thank you for any assistance,
> > 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.
>
> ______________________________________________
> 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