[R] Simple Stacking of Two Columns
Kimmo Elo
k|mmo@e|o @end|ng |rom utu@||
Tue Apr 4 10:08:51 CEST 2023
Hi,
or maybe this?
NamesLong<-data.frame(Names=unlist(NamesWide), row.names = NULL)
HTH,
Kimmo
ma, 2023-04-03 kello 16:23 +0000, Ebert,Timothy Aaron kirjoitti:
> My first thought was pivot_longer, and stack() is new to me.
> How about append(c1,c2) as another solution? Or
> data.frame(append(c1,c2)) if you want that form.
>
> Tim
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Marc
> Schwartz via R-help
> Sent: Monday, April 3, 2023 11:44 AM
> To: Sparks, John <jspark4 using uic.edu>; r-help using r-project.org
> Subject: Re: [R] Simple Stacking of Two Columns
>
> [External Email]
>
> Hi,
>
> You were on the right track using stack(), but you just pass the
> entire data frame as a single object, not the separate columns:
>
> > stack(NamesWide)
> values ind
> 1 Tom Name1
> 2 Dick Name1
> 3 Larry Name2
> 4 Curly Name2
>
> Note that stack also returns the index (second column of 'ind'
> values), which tells you which column in the source data frame the
> stacked values originated from.
>
> Thus, if you just want the actual data:
>
> > stack(NamesWide)$values
> [1] "Tom" "Dick" "Larry" "Curly"
>
> returns a vector, or:
>
> > stack(NamesWide)[, 1, drop = FALSE]
> values
> 1 Tom
> 2 Dick
> 3 Larry
> 4 Curly
>
> which returns a data frame with a single column named 'values'.
>
> Regards,
>
> Marc Schwartz
>
>
> On April 3, 2023 at 11:08:59 AM, Sparks, John
> (jspark4 using uic.edu (mailto:jspark4 using uic.edu)) wrote:
>
> > Hi R-Helpers,
> >
> > Sorry to bother you, but I have a simple task that I can't figure
> > out how to do.
> >
> > For example, I have some names in two columns
> >
> > NamesWide<-
> > data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))
> >
> > and I simply want to get a single column
> > NamesLong<-data.frame(Names=c("Tom","Dick","Larry","Curly"))
> > > NamesLong
> > Names
> > 1 Tom
> > 2 Dick
> > 3 Larry
> > 4 Curly
> >
> >
> > Stack produces an error
> > NamesLong<-stack(NamesWide$Name1,NamesWide$Names2)
> > Error in if (drop) { : argument is of length zero
> >
> > So does bind_rows
> > > NamesLong<-dplyr::bind_rows(NamesWide$Name1,NamesWide$Name2)
> > Error in `dplyr::bind_rows()`:
> > ! Argument 1 must be a data frame or a named atomic vector.
> > Run `rlang::last_error()` to see where the error occurred.
> >
> > I tried making separate dataframes to get around the error in
> > bind_rows but it puts the data in two different columns
> > Name1<-data.frame(c("Tom","Dick"))
> > Name2<-data.frame(c("Larry","Curly"))
> > NamesLong<-dplyr::bind_rows(Name1,Name2)
> > > NamesLong
> > c..Tom....Dick.. c..Larry....Curly..
> > 1 Tom
> > 2 Dick
> > 3 Larry
> > 4 Curly
> >
> > gather makes no change to the data
> > NamesLong<-gather(NamesWide,Name1,Name2)
> > > NamesLong
> > Name1 Name2
> > 1 Tom Larry
> > 2 Dick Curly
> >
> >
> > Please help me solve what should be a very simple problem.
> >
> > Thanks,
> > John Sparks
> >
> >
> >
> >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=MEaORpaFihsIHu3Iu2GwO15ey%2BvZP3Wxa6UiS3g0PyQ%3D&reserved=0
> > PLEASE do read the posting guide
> > https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=ZwKZkaGoEVMu8Jp%2BbcIj%2FLVi9%2Fwug%2Fi48uarb8yg5KY%3D&reserved=0
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=MEaORpaFihsIHu3Iu2GwO15ey%2BvZP3Wxa6UiS3g0PyQ%3D&reserved=0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=ZwKZkaGoEVMu8Jp%2BbcIj%2FLVi9%2Fwug%2Fi48uarb8yg5KY%3D&reserved=0
> 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.
More information about the R-help
mailing list