Search code examples
rstring-concatenation

Concatenating multiple rows with similar names in R


I have a dataframe db1 with say 30 variables. Out of these 30, ten have sequential names - X1, X2,....X10. All these X variables are characters. I wanted to concatenate all of them. So I could of course do

db1$new <- paste(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10)

But, this is not fun, and if I have a new file with different number of X variables, this code will not work. So, I need some method that concatenates using the variable name. I tried

zz1 <- paste(grep('^X',names(db1), value = TRUE))
zz2 <- paste("db1$",zz1,sep="",collapse = ",")

The second statement is to get the variable names seperated by commas. I then tried merging using

db1$new <- paste(db1$Terms,zz2,collapse = ","))

This did not work as R did not understand the zz2 were file names. What can I do?


Solution

  • Use do.call with paste0, like this, Using the dataset like below(Using @MKR data):

    df <- structure(list(id = 1:2, X1 = c("a", "b"), X2 = c("a", "b"), 
            X3 = c("a", "b")), .Names = c("id", 
        "X1", "X2", "X3"), row.names = c(NA, -2L), class = "data.frame")
    
    df$pastecol = do.call("paste0",df[,grep("^X\\d+$",names(df))])
    

    Output:

    #> df$pastecol = do.call("paste0",df[,grep("^X\\d+$",names(df))])
    #> df
    #  id X1 X2 X3 pastecol
    #1  1  a  a  a      aaa
    #2  2  b  b  b      bbb