[R] concatenating columns in data.frame

Eric Berger er|cjberger @end|ng |rom gm@||@com
Thu Jul 1 21:29:44 CEST 2021


Lovely one-liner Bert. Chapeau

On Thu, Jul 1, 2021 at 10:16 PM Berry, Charles <ccberry using health.ucsd.edu>
wrote:

>
>
> > On Jul 1, 2021, at 11:24 AM, Bert Gunter <bgunter.4567 using gmail.com> wrote:
> >
> > Why not simply:
> >
> > ## reprex
> > set.seed(123)
> > df = data.frame("A"=sample(letters, 10), "B"=sample(letters, 10),
> >                "C"=sample(letters,10), "D"=sample(letters, 10))
> > df
> > use_columns = c("D", "B")
> >
> > ## one liner
> > df$combo_col <- do.call(paste,c(df[,use_columns], sep = "_"))
> > df
> >
> > In case you are wondering, this works because by definition *a date
> > frame **is** a list*, so the concatenation is list concatenation.
> >
>
> Why not?
>
> Because I erroneously thought that there is a "data.frame' method for `c`
> and that this would cause a problem.
>
> But I was wrong, so your solution wins.
>
> Best,
> Chuck
> ______________________________________________
> 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