我一直在努力研究如何通过保持原始结构将表转换为数据框。
这是我正在谈论的表:
sample_size <- structure(c(0L, 2L, 2L, 0L, 3L, 1L, 3L, 9L, 13L, 0L, 0L, 0L,
0L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L,
2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 6L, 0L, 9L, 2L, 0L, 0L, 1L,
0L, 0L, 8L, 0L, 6L, 1L, 0L, 0L, 2L, 1L, 3L, 16L, 10L, 0L, 2L,
0L, 0L, 6L, 33L, 4L, 30L, 18L, 3L, 0L, 14L, 1L, 12L, 40L, 1L,
13L, 9L, 0L, 0L, 0L, 0L, 11L, 25L, 1L, 5L, 2L, 0L, 2L, 0L, 0L,
1L, 16L, 2L, 17L, 1L, 0L, 2L, 0L, 0L, 2L, 19L, 2L, 3L, 1L, 0L,
1L, 0L, 3L, 4L, 17L, 0L, 2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L,
2L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 2L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 2L, 0L, 2L, 4L, 1L, 0L, 2L, 0L, 0L, 2L, 0L,
3L, 7L, 2L, 4L, 10L, 16L, 4L, 2L, 0L, 22L, 11L, 31L, 7L, 8L,
18L, 3L, 3L, 1L, 21L, 8L, 31L, 4L, 10L, 11L, 3L, 2L, 0L, 19L,
5L, 29L, 4L, 6L, 16L, 2L, 1L, 2L, 11L, 12L, 27L, 6L, 7L, 14L,
2L, 0L, 2L, 14L, 9L, 35L), dim = c(9L, 11L, 2L), dimnames = structure(list(
c("Ardipithecus ramidus", "Australopithecus afarensis", "Australopithecus africanus",
"Australopithecus anamensis", "Early Homo", "Homo erectus",
"Homo habilis", "Paranthropus boisei", "Paranthropus robustus"
), c("C", "dc", "dm1", "dm2", "I1", "I2", "M1", "M2", "M3",
"P3", "P4"), c("L", "U")), names = c("", "", "")), class = "table")
Run Code Online (Sandbox Code Playgroud)
当我看到该对象时sample_size
,它看起来像这样:
, , = L
C dc dm1 dm2 I1 I2 M1 M2 M3 P3 P4
Ardipithecus ramidus 0 0 1 0 0 0 3 4 1 1 2
Australopithecus afarensis 2 0 2 2 6 8 16 30 13 5 17
Australopithecus africanus 2 0 0 0 0 0 10 18 9 2 1
Australopithecus anamensis 0 0 0 0 9 6 0 3 0 0 0
Early Homo 3 1 2 2 2 1 2 0 0 2 2
Homo erectus 1 0 0 0 0 0 0 14 0 0 0
Homo habilis 3 0 0 0 0 0 0 1 0 0 0
Paranthropus boisei 9 0 0 0 1 2 6 12 11 1 2
Paranthropus robustus 13 0 0 0 0 1 33 40 25 16 19
, , = U
C dc dm1 dm2 I1 I2 M1 M2 M3 P3 P4
Ardipithecus ramidus 2 0 0 0 1 0 4 7 4 4 6
Australopithecus afarensis 3 2 2 2 1 2 10 8 10 6 7
Australopithecus africanus 1 0 0 0 0 0 16 18 11 16 14
Australopithecus anamensis 0 0 0 0 0 0 4 3 3 2 2
Early Homo 1 2 1 2 2 2 2 3 2 1 0
Homo erectus 0 0 0 0 0 0 0 1 0 2 2
Homo habilis 3 0 0 0 2 3 22 21 19 11 14
Paranthropus boisei 4 0 0 0 4 7 11 8 5 12 9
Paranthropus robustus 17 0 0 0 1 2 31 31 29 27 35
Run Code Online (Sandbox Code Playgroud)
但是,当我转换sample_size
为数据帧时,它会改变结构。
sample_size_df <- as.data.frame(sample_size)
head(sample_size_df)
Var1 Var2 Var3 Freq
1 Ardipithecus ramidus C L 0
2 Australopithecus afarensis C L 2
3 Australopithecus africanus C L 2
4 Australopithecus anamensis C L 0
5 Early Homo C L 3
6 Homo erectus C L 1
Run Code Online (Sandbox Code Playgroud)
如何通过保留表中显示的原始结构将原始表转换为数据框?
当原始对象继承自 时,R 的 S3 调度倾向于执行该转换"table"
;我们可以通过具体指定来强制进行我认为您想要的转换as.data.frame.matrix
。由于假设为 2-dim(并且您的数据为 3),因此我们需要对每个平面(使用apply
和 it MARGIN=3
)以及rbind
最终的它们执行此操作。不过,我们需要小心,因为您的行名称在每个平面中都是重复的,因此我们会将它们放入“真实”列中,然后添加一列,以便您知道数据来自哪个平面。
这是一种混合的 base/tibble/dplyr 方法,有很多方法可以影响每个步骤:
apply(sample_size, 3, as.data.frame.matrix, simplify = FALSE) |>
lapply(tibble::rownames_to_column) |>
dplyr::bind_rows(.id = "plane")
# plane rowname C dc dm1 dm2 I1 I2 M1 M2 M3 P3 P4
# 1 L Ardipithecus ramidus 0 0 1 0 0 0 3 4 1 1 2
# 2 L Australopithecus afarensis 2 0 2 2 6 8 16 30 13 5 17
# 3 L Australopithecus africanus 2 0 0 0 0 0 10 18 9 2 1
# 4 L Australopithecus anamensis 0 0 0 0 9 6 0 3 0 0 0
# 5 L Early Homo 3 1 2 2 2 1 2 0 0 2 2
# 6 L Homo erectus 1 0 0 0 0 0 0 14 0 0 0
# 7 L Homo habilis 3 0 0 0 0 0 0 1 0 0 0
# 8 L Paranthropus boisei 9 0 0 0 1 2 6 12 11 1 2
# 9 L Paranthropus robustus 13 0 0 0 0 1 33 40 25 16 19
# 10 U Ardipithecus ramidus 2 0 0 0 1 0 4 7 4 4 6
# 11 U Australopithecus afarensis 3 2 2 2 1 2 10 8 10 6 7
# 12 U Australopithecus africanus 1 0 0 0 0 0 16 18 11 16 14
# 13 U Australopithecus anamensis 0 0 0 0 0 0 4 3 3 2 2
# 14 U Early Homo 1 2 1 2 2 2 2 3 2 1 0
# 15 U Homo erectus 0 0 0 0 0 0 0 1 0 2 2
# 16 U Homo habilis 3 0 0 0 2 3 22 21 19 11 14
# 17 U Paranthropus boisei 4 0 0 0 4 7 11 8 5 12 9
# 18 U Paranthropus robustus 17 0 0 0 1 2 31 31 29 27 35
Run Code Online (Sandbox Code Playgroud)
如果你只想保持它的基本R,那么有点不那么优雅:
tmp <- apply(sample_size, 3, as.data.frame.matrix, simplify = FALSE) |>
lapply(tibble::rownames_to_column)
Map(transform, tmp, plane = names(tmp)) |>
do.call(rbind.data.frame, args = _)
Run Code Online (Sandbox Code Playgroud)
(因为我使用args=_
基础 R 的|>
本机管道,所以这需要 R >= 4.2.0;如果您有较低版本,那么您可以切换到magrittr::%>%
和 它的.
代词,或者我们可以采取进一步的措施来缓解,lmk。)
作为替代方案,您可以使用展平数组并将其重塑为宽格式:
\nlibrary(tidyr)\n\nas.data.frame(sample_size) %>%\n pivot_wider(names_from = Var2, values_from = Freq)\n\n# A tibble: 18 \xc3\x97 13\n Var1 Var3 C dc dm1 dm2 I1 I2 M1 M2 M3 P3 P4\n <fct> <fct> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>\n 1 Ardipithecus ramidus L 0 0 1 0 0 0 3 4 1 1 2\n 2 Australopithecus afarensis L 2 0 2 2 6 8 16 30 13 5 17\n 3 Australopithecus africanus L 2 0 0 0 0 0 10 18 9 2 1\n 4 Australopithecus anamensis L 0 0 0 0 9 6 0 3 0 0 0\n 5 Early Homo L 3 1 2 2 2 1 2 0 0 2 2\n 6 Homo erectus L 1 0 0 0 0 0 0 14 0 0 0\n 7 Homo habilis L 3 0 0 0 0 0 0 1 0 0 0\n 8 Paranthropus boisei L 9 0 0 0 1 2 6 12 11 1 2\n 9 Paranthropus robustus L 13 0 0 0 0 1 33 40 25 16 19\n10 Ardipithecus ramidus U 2 0 0 0 1 0 4 7 4 4 6\n11 Australopithecus afarensis U 3 2 2 2 1 2 10 8 10 6 7\n12 Australopithecus africanus U 1 0 0 0 0 0 16 18 11 16 14\n13 Australopithecus anamensis U 0 0 0 0 0 0 4 3 3 2 2\n14 Early Homo U 1 2 1 2 2 2 2 3 2 1 0\n15 Homo erectus U 0 0 0 0 0 0 0 1 0 2 2\n16 Homo habilis U 3 0 0 0 2 3 22 21 19 11 14\n17 Paranthropus boisei U 4 0 0 0 4 7 11 8 5 12 9\n18 Paranthropus robustus U 17 0 0 0 1 2 31 31 29 27 35\n
Run Code Online (Sandbox Code Playgroud)\n或者使用基础reshape()
(名称需要稍微清理):
reshape(\n as.data.frame(sample_size),\n idvar = c("Var1", "Var3"),\n timevar = "Var2",\n direction = "wide"\n)\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
110 次 |
最近记录: |