简体   繁体   中英

Merge 10 variables in pairs and make them into one variable

This is how my files looks like :

#Var1    Var2   Var3  Var4
#0         1    2       1

I want something like this :

#Var1     Var2
#01        21

I am sorry for any inconvenience, I tried to be as clear as possible in asking this question. I am very new to R and stackoverflow. I really appreciate any help.

Just another way (assuming tt is your data.frame ):

sapply(seq(1, ncol(tt), by=2), function(x) do.call(paste0, c(tt[,x:(x+1)])))
# [1] "01" "21"

(or) as @Sven shows under comment:

setNames(as.data.frame(lapply(seq(1, ncol(tt), by=2), 
         function(x) do.call(paste0, c(tt[,x:(x+1)])))), names(tt)[1:2])
# The data
dat <- data.frame(0, 1, 2, 1)

# Transform data
as.data.frame(lapply(c(1, 3), function(x) paste(dat[c(x, x+1)], collapse = "")))

  X.01. X.21.
1    01    21

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM