user3253470
user3253470

Reputation: 201

How to merge multiples columns of a table into 1 in R

I have a data frame (control.sub) containing multiple columns (t1,t2,t3,t4,t5,t6). I want to merge all these columns into one, also NA should be removed.

> control.sub
                             t1                         t2                         t3                         t4
29   5500024017802120306174.H01 5500024017802120306174.G02 5500024017802120306174.E03 5500024017802120306174.D04
810  5500024030401071707292.H01 5500024030401071707292.G02 5500024030401071707292.E03 5500024030401071707292.D04
4693 5500024035736031208612.G08 5500024035736031208612.E09 5500024035736031208612.D10 5500024035736031208612.B11
                             t5                         t6
29   5500024017802120306174.B05 5500024017802120306174.A06
810  5500024030401071707292.B05 5500024030401071707292.A06
4693 5500024035736031208612.A12                       <NA>

I want the final outcome as:

> control.sub
                                 t1
    29   5500024017802120306174.H01 5500024017802120306174.G02 5500024017802120306174.E03 5500024017802120306174.D04
    810  5500024030401071707292.H01 5500024030401071707292.G02 5500024030401071707292.E03 5500024030401071707292.D04
    4693 5500024035736031208612.G08 5500024035736031208612.E09 5500024035736031208612.D10 5500024035736031208612.B11

       5500024017802120306174.B05 5500024017802120306174.A06
      5500024030401071707292.B05 5500024030401071707292.A06
     5500024035736031208612.A12

One columns (t1) containing all values.

Upvotes: 0

Views: 99

Answers (2)

Biebiep
Biebiep

Reputation: 83

The following code works, but I don't know if anyone would consider it "optimal":

var <- as.vector(do.call('c',control.sub))

I would suggest going up higher in your code where you generate control.sub (if that is possible) and then manipulate the output format there.

If your variables are factors(you can check this by running:)

sapply(control.sub,class)

then you should first run:

controlsub<-lapply(control.sub,as.character)

EDIT: this is better:

 var <- unlist(control.sub)

Upvotes: 0

C8H10N4O2
C8H10N4O2

Reputation: 19005

slightly more reproducible example:

df <- data.frame(t1 = c(letters[1:5],NA), t2 = c(NA, LETTERS[6:10]), 
                 t3 = c(11:12,NA,13:15), stringsAsFactors=FALSE)
df
#     t1   t2 t3
# 1    a <NA> 11
# 2    b    F 12
# 3    c    G NA
# 4    d    H 13
# 5    e    I 14
# 6 <NA>    J 15


df2 <- data.frame(t1 = apply(df, 1, function(s) paste(s[!is.na(s)], collapse=" ")) )

df2
#       t1
# 1   a 11
# 2 b F 12
# 3    c G
# 4 d H 13
# 5 e I 14
# 6   J 15

EDIT

I think the OP is looking for this, but his/her example is wrong:

unlist_not_na <- function(df){
  v <- unlist(df)
  v[!is.na(v)]
}
df3 <- data.frame(t1 = unlist_not_na(df))

df3
#     t1
# t11  a
# t12  b
# t13  c
# t14  d
# t15  e
# t22  F
# t23  G
# t24  H
# t25  I
# t26  J
# t31 11
# t32 12
# t34 13
# t35 14
# t36 15

Upvotes: 1

Related Questions