R - 将数据帧合并为 table,用斜杠 ("/") 分隔值
R - Combine data frames to a table, separating values with a slash ("/")
我正在使用如下所示的数据框:
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")
我想将它们组合成一个 table(或数据框),值之间用斜杠(“/”)或括号分隔。然后我将它保存为.xls文件并将table复制到word.
最终结果将是这样的(为了简单起见,我只显示了第一列):
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
我如何使用 R 实现这一点?
试试这个:
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"
我们可以通过 unlist
两个数据集然后 paste
来做到这一点
res <- tbl45
res[] <- paste(unlist(tbl45), unlist(tbl85), sep='/')
res
# 2010's 2020's 2030's 2040's 2050's
#[0,100] 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
#(200,300] 33.33/31.4 31.88/36.23 27.54/32.37 33.33/30.43 33.33/32.85
#(300,400] 25.6/30.43 36.71/29.95 34.3/28.02 26.57/33.33 26.09/30.43
#(400,500] 32.37/34.78 28.5/30.43 33.33/34.3 28.5/31.4 32.85/29.47
#(500,600] 6.76/1.45 1.45/1.45 2.42/2.9 9.18/1.45 5.31/3.86
# 2060's 2070's 2080's 2090's
#[0,100] 0.48/0.48 0.48/0.48 0.48/0.48 0.48/1.45
#(100,200] 3.38/4.83 1.93/5.8 2.42/5.8 2.42/8.21
#(200,300] 25.6/33.33 33.82/31.88 34.3/35.27 31.4/38.16
#(300,400] 32.37/30.43 28.99/36.23 31.4/33.82 33.33/32.85
#(400,500] 36.23/26.57 31.4/24.15 28.99/23.19 29.95/17.87
#(500,600] 1.93/4.35 3.38/1.45 2.42/1.45 2.42/1.45
我正在使用如下所示的数据框:
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")
我想将它们组合成一个 table(或数据框),值之间用斜杠(“/”)或括号分隔。然后我将它保存为.xls文件并将table复制到word.
最终结果将是这样的(为了简单起见,我只显示了第一列):
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
我如何使用 R 实现这一点?
试试这个:
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"
我们可以通过 unlist
两个数据集然后 paste
res <- tbl45
res[] <- paste(unlist(tbl45), unlist(tbl85), sep='/')
res
# 2010's 2020's 2030's 2040's 2050's
#[0,100] 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
#(200,300] 33.33/31.4 31.88/36.23 27.54/32.37 33.33/30.43 33.33/32.85
#(300,400] 25.6/30.43 36.71/29.95 34.3/28.02 26.57/33.33 26.09/30.43
#(400,500] 32.37/34.78 28.5/30.43 33.33/34.3 28.5/31.4 32.85/29.47
#(500,600] 6.76/1.45 1.45/1.45 2.42/2.9 9.18/1.45 5.31/3.86
# 2060's 2070's 2080's 2090's
#[0,100] 0.48/0.48 0.48/0.48 0.48/0.48 0.48/1.45
#(100,200] 3.38/4.83 1.93/5.8 2.42/5.8 2.42/8.21
#(200,300] 25.6/33.33 33.82/31.88 34.3/35.27 31.4/38.16
#(300,400] 32.37/30.43 28.99/36.23 31.4/33.82 33.33/32.85
#(400,500] 36.23/26.57 31.4/24.15 28.99/23.19 29.95/17.87
#(500,600] 1.93/4.35 3.38/1.45 2.42/1.45 2.42/1.45