Does anyone know of a method in R to concatenate n columns but only keep the separator if there is a value in that row? If you run the example below:
df <- data.frame(
name1 = c("Jim","Bob","Sue"),
name2 = c("Jane","","Bane"),
name3 = c('Conor',"",""),
name4 = c("","","Bonor")
)
df$names <- paste(df$name1,df$name2,df$name3, sep=";")
You will see that separators are included at the end and in-between values even though the cells are empty, with the output:
df =
name1 name2 name3 name4 names
Jim Jane Conor Jim;Jane;Conor;
Bob Bob;;;
Sue Bane Bonor Sue;Bane;;Bonor
Is there any method to either not include or drop separators in the case where the cells are empty? With the desired outcome:
df =
name1 name2 name3 name4 names
Jim Jane Conor Jim;Jane;Conor
Bob Bob
Sue Bane Bonor Sue;Bane;Bonor