Paste the elements of two columns

224 Views Asked by At

I have a data.frame of the following kind

set.seed(12)
d = data.frame(a=sample(5,x=1:9), 
               b=sample(5,x=1:9),
               c=sample(5,x=1:9),
               d=sample(5,x=1:9),
               e=sample(5,x=1:9),
               f=sample(5,x=1:9))

d
#   a b c d e f
# 1 1 1 4 4 2 3
# 2 7 2 7 9 7 5
# 3 8 5 3 8 1 2
# 4 2 9 8 7 5 9
# 5 9 6 2 1 9 4

I would like to take the first two columns, convert the integer into characters and paste the two elements of the same row together. Then repeat the process each successive pair of columns.

Here is a script that would do the job correctly:

bar = function (twocols) {sapply(1:nrow(twocols), FUN=function(x) {paste(twocols[x,], collapse="")} )}

    count = 0
    out = matrix(0, ncol=ncol(d)/2, nrow=nrow(d))
    for (i in seq(1,ncol(d), 2)) {
       count = count+1
       out[,count] = bar(d[,i:(i+1)])
    }

print(out)
     [,1] [,2] [,3]
[1,] "11" "44" "23"
[2,] "72" "79" "75"
[3,] "85" "38" "12"
[4,] "29" "87" "59"
[5,] "96" "21" "94"

But my data.frame is actually very big and looping through the whole data.frame in R is very slow. Do you have a more efficient solution? Rcpp might be the solution but I don't know how to code in C++.

2

There are 2 best solutions below

1
On BEST ANSWER

This matches your description, but not the output you show:

mat = as.matrix(d)

matrix(paste0(mat[, seq(1, ncol(mat), by = 2)],
              mat[, seq(2, ncol(mat), by = 2)]),
       ncol = ncol(mat) / 2)

#      [,1] [,2] [,3]
# [1,] "11" "44" "23"
# [2,] "72" "79" "75"
# [3,] "85" "38" "12"
# [4,] "29" "87" "59"
# [5,] "96" "21" "94"

You could, of course, convert the result to numeric, back to a data.frame, etc.

2
On

Try:

m <- as.matrix(10*d[c(T,F)]+d[c(F,T)])
m[] <- as.character(m)