ShareRough
ShareRough

Reputation: 25

R concatenate every row for each column in a data frame

I have the following data.frame

> df <- data.frame(v1,v2)
> df
   v1  v2  
1 aaa 111 
2 bbb 222 

And i would like to concatenate each row into their respective column resulting in a data.frame like the following:

> df
      v1       v2
 1 aaa, bbb 111, 222

Upvotes: 0

Views: 55

Answers (1)

akrun
akrun

Reputation: 886938

We can use toString by looping over the columns

data.frame(lapply(df, toString))

Or in tidyverse

library(dplyr)
df %>%
   summarise_all(toString)
#        v1       v2
#1 aaa, bbb 111, 222

Upvotes: 1

Related Questions