Insert columns from two data frames

I want to occasionally insert columns from different data frames (tables, matrices, or something else). For example, I have a tool table and a stan devs table. I want both to be inserted along with sd in parentheses for latex printing. I suspect there is a friendly plyr solution, but I can’t figure out how to work with two data frames (I tried to save the data as a list and use ldply, but this was my first attempt with the plyr list function, and it fell in flames.

Thanks in advance.

#========= #fake data #========= x<-mtcars[1:3,1:3] y<-mtcars[1:3,8:10] #========================== #A column pasting function #========================== meansd<-function(x, y){ x<-round(x, dig=2) y<-round(y, dig=2) paste(x, "(", y, ")", sep="") } 

What i got.

DESIRED RESULTS No column names required. I don't care if returning is a matrix or a data framework.

 16.46(0) 0(1) 1(4) 17.02(0) 0(1) 1(4) 18.61(1) 1(1) 1(4) 
+6
source share
3 answers

Here is your function edited to scroll and insert each column. This gives the desired result, but there is probably a smarter way to do it.

 meansd<-function(x, y){ x<-round(x, digits = 2) y<-round(y, digits = 2) out <- matrix(ncol = dim(x)[1], nrow = dim(x)[2]) for(i in 1:dim(x)[1]) { out[i, ] <-paste(x[i, ], "(", y[i, ], ")", sep="") } return(out) } 
+4
source

Below is the approach using plyr

 t(ldply(1:NCOL(x), function(i) meansd(x[,i], y[,i]))) 
+7
source

How about mapply?

 x <- mtcars[1:3,1:3] y <- mtcars[1:3,8:10] mypaste <- function(x,y) paste(x, "(", y, ")", sep="") mapply(mypaste, x,y) mpg cyl disp [1,] "21(0)" "6(1)" "160(4)" [2,] "21(0)" "6(1)" "160(4)" [3,] "22.8(1)" "4(1)" "108(4)" 
+7
source

Source: https://habr.com/ru/post/900658/


All Articles