[R] concatenating columns in data.frame

Berry, Charles ccberry @end|ng |rom he@|th@uc@d@edu
Thu Jul 1 21:15:19 CEST 2021



> 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


More information about the R-help mailing list