Search code examples
rmergedataframeseparator

R - Combine data frames to a table, separating values with a slash ("/")


I am working with the data frames shown below:

tbl45 <- structure(list(`2010's` = c(0.48, 1.45, 33.33, 25.6, 32.37, 6.76
), `2020's` = c(0.48, 0.97, 31.88, 36.71, 28.5, 1.45), `2030's` = c(0.48, 
1.93, 27.54, 34.3, 33.33, 2.42), `2040's` = c(0.48, 1.93, 33.33, 
26.57, 28.5, 9.18), `2050's` = c(0.48, 1.93, 33.33, 26.09, 32.85, 
5.31), `2060's` = c(0.48, 3.38, 25.6, 32.37, 36.23, 1.93), `2070's` = c(0.48, 
1.93, 33.82, 28.99, 31.4, 3.38), `2080's` = c(0.48, 2.42, 34.3, 
31.4, 28.99, 2.42), `2090's` = c(0.48, 2.42, 31.4, 33.33, 29.95, 
2.42)), .Names = c("2010's", "2020's", "2030's", "2040's", "2050's", 
"2060's", "2070's", "2080's", "2090's"), row.names = c("[0,100]", 
"(100,200]", "(200,300]", "(300,400]", "(400,500]", "(500,600]"
), class = "data.frame")

tbl85 <- structure(list(`2010's` = c(0.48, 1.45, 31.4, 30.43, 34.78, 1.45
), `2020's` = c(0.48, 1.45, 36.23, 29.95, 30.43, 1.45), `2030's` = c(0.48, 
1.93, 32.37, 28.02, 34.3, 2.9), `2040's` = c(0.48, 2.9, 30.43, 
33.33, 31.4, 1.45), `2050's` = c(0.48, 2.9, 32.85, 30.43, 29.47, 
3.86), `2060's` = c(0.48, 4.83, 33.33, 30.43, 26.57, 4.35), `2070's` = c(0.48, 
5.8, 31.88, 36.23, 24.15, 1.45), `2080's` = c(0.48, 5.8, 35.27, 
33.82, 23.19, 1.45), `2090's` = c(1.45, 8.21, 38.16, 32.85, 17.87, 
1.45)), .Names = c("2010's", "2020's", "2030's", "2040's", "2050's", 
"2060's", "2070's", "2080's", "2090's"), row.names = c("[0,100]", 
"(100,200]", "(200,300]", "(300,400]", "(400,500]", "(500,600]"
), class = "data.frame")

and I would like to combine them in one single table (or data frame), with the values separated by a slash ("/") or parenthesis. Then I will save it as a .xls file and copy the table to word.

The final result would be something like this (I am showing only the first column for the simplicity sake):

               2010's 
[0,100]     0.48 / 0.48 
(100,200]   1.45 / 1.45
(200,300]  33.33 / 31.40
(300,400]  25.60 / 30.43
(400,500]  32.37 / 34.78
(500,600]   6.76 / 1.45

How can I achieve that using R?


Solution

  • Try this:

    res <- mapply(function(x,y) paste(x,y, sep = "/"), tbl45, tbl85)
    rownames(res) <- rownames(tbl45)
    res
              2010's        2020's        2030's        2040's        2050's        2060's       
    [0,100]   "0.48/0.48"   "0.48/0.48"   "0.48/0.48"   "0.48/0.48"   "0.48/0.48"   "0.48/0.48"  
    (100,200] "1.45/1.45"   "0.97/1.45"   "1.93/1.93"   "1.93/2.9"    "1.93/2.9"    "3.38/4.83"  
    (200,300] "33.33/31.4"  "31.88/36.23" "27.54/32.37" "33.33/30.43" "33.33/32.85" "25.6/33.33" 
    (300,400] "25.6/30.43"  "36.71/29.95" "34.3/28.02"  "26.57/33.33" "26.09/30.43" "32.37/30.43"
    (400,500] "32.37/34.78" "28.5/30.43"  "33.33/34.3"  "28.5/31.4"   "32.85/29.47" "36.23/26.57"
    (500,600] "6.76/1.45"   "1.45/1.45"   "2.42/2.9"    "9.18/1.45"   "5.31/3.86"   "1.93/4.35"  
              2070's        2080's        2090's       
    [0,100]   "0.48/0.48"   "0.48/0.48"   "0.48/1.45"  
    (100,200] "1.93/5.8"    "2.42/5.8"    "2.42/8.21"  
    (200,300] "33.82/31.88" "34.3/35.27"  "31.4/38.16" 
    (300,400] "28.99/36.23" "31.4/33.82"  "33.33/32.85"
    (400,500] "31.4/24.15"  "28.99/23.19" "29.95/17.87"
    (500,600] "3.38/1.45"   "2.42/1.45"   "2.42/1.45"