r - How to combine two or more columns in a dataframe into a new column with a new name? -
for example if have this:
n = c(2, 3, 5) s = c("aa", "bb", "cc") b = c(true, false, true) df = data.frame(n, s, b) n s b 1 2 aa true 2 3 bb false 3 5 cc true
then how combine 2 columns n , s new column named x such looks this:
n s b x 1 2 aa true 2 aa 2 3 bb false 3 bb 3 5 cc true 5 cc
use paste
.
df$x <- paste(df$n,df$s) df # n s b x # 1 2 aa true 2 aa # 2 3 bb false 3 bb # 3 5 cc true 5 cc
Comments
Post a Comment