Paste the elements of two columns [duplicate]

孤街浪徒 提交于 2020-01-11 12:26:07

问题


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++.


回答1:


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:


Try:

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


来源:https://stackoverflow.com/questions/30927899/paste-the-elements-of-two-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!