How can multiple columns be merged into one column in R language?
You can combine multiple columns into one using the unite() function. Here is an example:
Assume you have a dataframe called df with three columns col1, col2, and col3, and you want to merge these three columns into one column named combined.
library(dplyr)
df <- data.frame(col1 = c("a", "b", "c"),
col2 = c("d", "e", "f"),
col3 = c("g", "h", "i"))
df <- unite(df, combined, col1, col2, col3, sep = "-")
The code above will combine col1, col2, and col3 into one column named combined, using “-” as the delimiter. The merged result will be as follows:
combined
1 a-d-g
2 b-e-h
3 c-f-i
Please note that you need to load the dplyr package before using the unite() function.