1

I have a dataframe as follows:

df <- structure(list(a = c("1", "2", "3"), b = c("Tom", "Jen", "Rob"
), c = c("Wrist", "Ankle", "Neck")), class = "data.frame", row.names = c(NA, 
-3L))

I'm trying to collapse my columns into a single column where each row contains a single text string comprised of data from each column separated by a delimiter (specifically a ; [colon]).

My current code converts my data into a text string but instead of multiple rows it's a single cell containing all data in the data frame:

data.frame(format_delim(df[1:nrow(df),],";", append = FALSE, col_names = FALSE, quote_escape = FALSE))

What should I do instead to obtain the following data frame?

structure(list(c..1.Tom.Wrist....2.Jen.Ankle....3.Rob.Neck.. = c("1;Tom;Wrist", 
"2;Jen;Ankle", "3;Rob;Neck")), class = "data.frame", row.names = c(NA, 
-3L))

Thanks. Hope this was easy to follow.

2 Answers2

1

Try this apply() solution:

#Code
out <- data.frame(v1=apply(df,1,function(x) paste0(x,collapse = ';')))

Output:

           v1
1 1;Tom;Wrist
2 2;Jen;Ankle
3  3;Rob;Neck

If you want to add the new variable to your original df you can use this:

#Code
df$var <- apply(df,1,function(x) paste0(x,collapse = ';')) 

Output:

  a   b     c         var
1 1 Tom Wrist 1;Tom;Wrist
2 2 Jen Ankle 2;Jen;Ankle
3 3 Rob  Neck  3;Rob;Neck
Duck
  • 39,058
  • 13
  • 42
  • 84
1

We can use vectorized option with do.call

data.frame(col1 = do.call(paste, c(df, sep=";")))
#     col1
#1 1;Tom;Wrist
#2 2;Jen;Ankle
#3  3;Rob;Neck

Or if there are only few columns, we can use

with(df, paste(a, b, c, sep=";"))

Or another option is unite

library(tidyr)
library(dplyr)
df %>%
    unite(col1, a, b, c, sep = ";")
#       col1
#1 1;Tom;Wrist
#2 2;Jen;Ankle
#3  3;Rob;Neck
akrun
  • 874,273
  • 37
  • 540
  • 662