Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Join all column values into one separate column

Input
Column1
a;b
c;d
e;f

Output
Column1 Column2
a;b            a;b,c;d,e;f
c;d
e;f

I want all the values of Column1 concatenated with a delimiter into one single column in one cell in Column2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With base R, we can initialize the column with NA. Then, we can turn the column into one string and remove the extra spaces and commas and replace with ;.

df$Column2 <- NA
df$Column2[1] <- gsub(", ", ";", toString(df$Column1))

Or here is a tidyverse option:

library(tidyverse)

df %>% 
  mutate(Column2 = ifelse(row_number()==1, str_replace_all(toString(Column1), ", ", ";"), NA))

Output

  Column1     Column2
1     a;b a;b;c;d;e;f
2     c;d        <NA>
3     e;f        <NA>

Data

df <-
  structure(list(Column1 = c("a;b", "c;d", "e;f")),
            class = "data.frame",
            row.names = c(NA,-3L))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading