0

This question is a little different from previous post. I have several columns of random indicator variables and I want to merge them into a sequence "without space". My data is like the following .

  ID COL1 COL2 COL3 COL4 COL5 COL6 COL7
1  1    a    b    c    d    e    f    g
2  2    a    b    c    d    e    f    g
3  3    a    b    c    d    e    f    g
4  4    a    b    c    d    e    f    g
5  5    a    b    c    d    e    f    g
6  6    a    b    c    d    e    f    g
7  7    a    b    c    d    e    f    g
8  8    a    b    c    d    e    f    g

ID<-c(1,2,3,4,5,6,7,8)
COL1<-c('a','a','a','a','a','a','a','a')
COL2<-c('b','b','b','b','b','b','b','b')
COL3<-c('c','c','c','c','c','c','c','c')
COL4<-c('d','d','d','d','d','d','d','d')
COL5<-c('e','e','e','e','e','e','e','e')
COL6<-c('f','f','f','f','f','f','f','f')
COL7<-c('g','g','g','g','g','g','g','g')
df<-data.frame(ID,COL1,COL2,COL3,COL4,COL5,COL6,COL7)

I want to get a column to simply merge other columns without space.

  ID COL1 COL2 COL3 COL4 COL5 COL6 COL7   col1to4     col1to7    col3to5
1  1    a    b    c    d    e    f    g      abcd     abcdefg        cde 
2  2    a    b    c    d    e    f    g      abcd     abcdefg        cde
3  3    a    b    c    d    e    f    g      abcd     abcdefg        cde
4  4    a    b    c    d    e    f    g      abcd     abcdefg        cde
5  5    a    b    c    d    e    f    g      abcd     abcdefg        cde
6  6    a    b    c    d    e    f    g      abcd     abcdefg        cde
7  7    a    b    c    d    e    f    g      abcd     abcdefg        cde
8  8    a    b    c    d    e    f    g      abcd     abcdefg        cde

Thanks in advance.

Terence Tien
  • 329
  • 1
  • 3
  • 15
  • Thanks for marked. The link is very helpful. It is a little different from the link. What I want is "no space" merge. – Terence Tien May 10 '16 at 21:54

2 Answers2

0
df$col1to4 = apply(df[ , grep("COL[1-4]", names(df))], 1, paste, collapse="")

or

 df$col1to4 = apply(df[, paste0("COL", 1:4)], 1, paste, collapse="")

The first one uses a regular expression and is more flexible, but if your names are very regular, you can save some typing with the second one. You can also refer to columns by their position, but that seems more prone to error.

And likewise for the others:

df$col1to7 = apply(df[ , grep("COL[1-7]", names(df))], 1, paste, collapse="")

df$col3to5 = apply(df[ , grep("COL[3-5]", names(df))], 1, paste, collapse="")
eipi10
  • 91,525
  • 24
  • 209
  • 285
0

This should work:

df$col1to4<-paste0(df$COL1,df$COL2,df$COL3,df$COL4)
df$col1to7<-paste0(df$COL1,df$COL2,df$COL3,df$COL4,df$COL5,df$COL6,df$COL7)
df$col3to5<-paste0(df$COL3,df$COL4,df$COL5)
Buzz Lightyear
  • 824
  • 1
  • 7
  • 18