通过将值从一个粘贴(添加)到另一个来组合数据框
Combine data frames by pasting (adding) values from one into the other
我有两个数据框(矩阵)。我想过去或将值加在一起,可能是通过添加括号。这是一个例子:
所以我有 'x' 和 'y'。我想将它们组合起来,这样我就有 'z'.
set.seed(1)
x <- matrix(sample.int(6, size = 2*3, replace = TRUE), nrow = 2, ncol = 3)
y <- matrix(sample.int(6, size = 2*3, replace = TRUE), nrow = 2, ncol = 3)
z <- data.frame(c("2(6)", "3(4)"), c("4(4)", "6(1)"), c("2(2)", "6(2)"))
colnames(z) <- c("A1", "A2", "A3")
你可以试试:
matrix(paste0(x, "(",y,")"), dim(x), dimnames = list(NULL, paste0("A", 1:3)))
A1 A2 A3
[1,] "2(6)" "4(4)" "2(2)"
[2,] "3(4)" "6(1)" "6(2)"
我有两个数据框(矩阵)。我想过去或将值加在一起,可能是通过添加括号。这是一个例子: 所以我有 'x' 和 'y'。我想将它们组合起来,这样我就有 'z'.
set.seed(1)
x <- matrix(sample.int(6, size = 2*3, replace = TRUE), nrow = 2, ncol = 3)
y <- matrix(sample.int(6, size = 2*3, replace = TRUE), nrow = 2, ncol = 3)
z <- data.frame(c("2(6)", "3(4)"), c("4(4)", "6(1)"), c("2(2)", "6(2)"))
colnames(z) <- c("A1", "A2", "A3")
你可以试试:
matrix(paste0(x, "(",y,")"), dim(x), dimnames = list(NULL, paste0("A", 1:3)))
A1 A2 A3
[1,] "2(6)" "4(4)" "2(2)"
[2,] "3(4)" "6(1)" "6(2)"