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
>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))