我们可以使用paste
data.frame(cell = as.integer(do.call(paste0, df1)))
-输出
# cell
#1 111
#2 112
#3 121
#4 122
#5 211
#6 212
#7 221
#8 222
注意:在可重现的示例中,我们使用 data.frame 作为输入。如果是matrix,则使用as.data.frame 转换为data.frame 并应用相同的代码
或使用unite
library(tidyr)
library(dplyr)
unite(df1, cell, everything(), sep="") %>%
type.convert(as.is = TRUE)
数据
df1 <- structure(list(A = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), B = c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L), C = c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L)), class = "data.frame", row.names = c("1", "2", "3", "4",
"5", "6", "7", "8"))