6.19 长宽转换
args("reshape")
## function (data, varying = NULL, v.names = NULL, timevar = "time",
## idvar = "id", ids = 1L:NROW(data), times = seq_along(varying[[1L]]),
## drop = NULL, direction, new.row.names = NULL, sep = ".",
## split = if (sep == "") {
## list(regexp = "[A-Za-z][0-9]", include = TRUE)
## } else {
## list(regexp = sep, include = FALSE, fixed = TRUE)
## })
## NULL
PlantGrowth 数据集的重塑操作也可以使用内置的函数 reshape()
实现
$id <- rep(1:10, 3)
PlantGrowth<- reshape(
dat data = PlantGrowth, idvar = "group", v.names = "weight",
timevar = "id", direction = "wide",
sep = ""
)::kable(dat,
knitrcaption = "不同生长环境下植物的干重", row.names = FALSE,
col.names = gsub("(weight)", "", names(dat)),
align = "c"
)
group | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
ctrl | 4.17 | 5.58 | 5.18 | 6.11 | 4.50 | 4.61 | 5.17 | 4.53 | 5.33 | 5.14 |
trt1 | 4.81 | 4.17 | 4.41 | 3.59 | 5.87 | 3.83 | 6.03 | 4.89 | 4.32 | 4.69 |
trt2 | 6.31 | 5.12 | 5.54 | 5.50 | 5.37 | 5.29 | 4.92 | 6.15 | 5.80 | 5.26 |
或者,我们也可以使用 tidyr 包提供的 pivot_wider()
函数
::pivot_wider(
tidyrdata = PlantGrowth, id_cols = id,
names_from = group, values_from = weight
)
## # A tibble: 10 × 4
## id ctrl trt1 trt2
## <int> <dbl> <dbl> <dbl>
## 1 1 4.17 4.81 6.31
## 2 2 5.58 4.17 5.12
## 3 3 5.18 4.41 5.54
## 4 4 6.11 3.59 5.5
## 5 5 4.5 5.87 5.37
## 6 6 4.61 3.83 5.29
## 7 7 5.17 6.03 4.92
## 8 8 4.53 4.89 6.15
## 9 9 5.33 4.32 5.8
## 10 10 5.14 4.69 5.26
或者,我们还可以使用 data.table 包提供的 dcast()
函数,用于将长格式的数据框重塑为宽格式的
<- as.data.table(PlantGrowth)
PlantGrowth_DT # 纵
dcast(PlantGrowth_DT, id ~ group, value.var = "weight")
## id ctrl trt1 trt2
## 1: 1 4.17 4.81 6.31
## 2: 2 5.58 4.17 5.12
## 3: 3 5.18 4.41 5.54
## 4: 4 6.11 3.59 5.50
## 5: 5 4.50 5.87 5.37
## 6: 6 4.61 3.83 5.29
## 7: 7 5.17 6.03 4.92
## 8: 8 4.53 4.89 6.15
## 9: 9 5.33 4.32 5.80
## 10: 10 5.14 4.69 5.26
# 横
dcast(PlantGrowth_DT, group ~ id, value.var = "weight")
## group 1 2 3 4 5 6 7 8 9 10
## 1: ctrl 4.17 5.58 5.18 6.11 4.50 4.61 5.17 4.53 5.33 5.14
## 2: trt1 4.81 4.17 4.41 3.59 5.87 3.83 6.03 4.89 4.32 4.69
## 3: trt2 6.31 5.12 5.54 5.50 5.37 5.29 4.92 6.15 5.80 5.26