【发布时间】:2021-02-01 14:06:40
【问题描述】:
我第一次在 R 中使用嵌套/取消嵌套函数,但我不明白结果。我嵌套并立即取消嵌套并比较之前/之后的数据帧。为什么数据帧不相同?
> library(tidyverse)
> concentration_original <- readRDS("./Data/concentration.Rds")
> print(concentration_original, n=15)
# A tibble: 12 x 5
SUBJID WT DOSE TIME CONC
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 79.6 4.02 0 0.74
2 1 79.6 4.02 0.25 2.84
3 1 79.6 4.02 0.570 6.57
4 1 79.6 4.02 1.12 10.5
5 1 79.6 4.02 2.02 9.66
6 1 79.6 4.02 3.82 8.58
7 2 72.4 4.4 0 0
8 2 72.4 4.4 0.27 1.72
9 2 72.4 4.4 0.52 7.91
10 2 72.4 4.4 1 8.31
11 2 72.4 4.4 1.92 8.33
12 2 72.4 4.4 3.5 6.85
>
> concentration_nested <- concentration_original %>% nest(data = c(TIME, CONC))
> concentration_nested
# A tibble: 2 x 4
SUBJID WT DOSE data
<dbl> <dbl> <dbl> <list>
1 1 79.6 4.02 <tibble [6 × 2]>
2 2 72.4 4.4 <tibble [6 × 2]>
>
> concentration_unnested <- unnest(concentration_nested, cols = c(data))
> print(concentration_unnested, n=15)
# A tibble: 12 x 5
SUBJID WT DOSE TIME CONC
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 79.6 4.02 0 0.74
2 1 79.6 4.02 0.25 2.84
3 1 79.6 4.02 0.570 6.57
4 1 79.6 4.02 1.12 10.5
5 1 79.6 4.02 2.02 9.66
6 1 79.6 4.02 3.82 8.58
7 2 72.4 4.4 0 0
8 2 72.4 4.4 0.27 1.72
9 2 72.4 4.4 0.52 7.91
10 2 72.4 4.4 1 8.31
11 2 72.4 4.4 1.92 8.33
12 2 72.4 4.4 3.5 6.85
>
> if (identical(concentration_unnested, concentration_original)) {
+ print("After nest/unnest, we have a dataframe which IS IDENTICAL to the original")
+ } else {
+ print("After nest/unnest, we have a dataframe which IS NOT IDENTICAL to the original")
+ }
[1] "After nest/unnest, we have a dataframe which IS NOT IDENTICAL to the original"
>
> all.equal(concentration_unnested, concentration_original)
[1] "Attributes: < Length mismatch: comparison on first 2 components >"
>
请注意,我使用 all.equal 是为了查看问题可能与属性有关。如果我改用 all_equal,结果为 TRUE,但我仍然坚持使用 identical 函数说数据帧不一样。感谢您对此的任何帮助!
添加了原始 df 和嵌套/未嵌套 df 的 dput。
> dput(concentration_original)
structure(list(SUBJID = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
WT = c(79.6, 79.6, 79.6, 79.6, 79.6, 79.6, 72.4, 72.4, 72.4,
72.4, 72.4, 72.4), DOSE = c(4.02, 4.02, 4.02, 4.02, 4.02,
4.02, 4.4, 4.4, 4.4, 4.4, 4.4, 4.4), TIME = c(0, 0.25, 0.57,
1.12, 2.02, 3.82, 0, 0.27, 0.52, 1, 1.92, 3.5), CONC = c(0.74,
2.84, 6.57, 10.5, 9.66, 8.58, 0, 1.72, 7.91, 8.31, 8.33,
6.85)), spec = structure(list(cols = list(SUBJID = structure(list(), class = c("collector_double",
"collector")), WT = structure(list(), class = c("collector_double",
"collector")), DOSE = structure(list(), class = c("collector_double",
"collector")), TIME = structure(list(), class = c("collector_double",
"collector")), CONC = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"), row.names = c(NA,
-12L), class = c("tbl_df", "tbl", "data.frame"))
> dput(concentration_unnested)
structure(list(SUBJID = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
WT = c(79.6, 79.6, 79.6, 79.6, 79.6, 79.6, 72.4, 72.4, 72.4,
72.4, 72.4, 72.4), DOSE = c(4.02, 4.02, 4.02, 4.02, 4.02,
4.02, 4.4, 4.4, 4.4, 4.4, 4.4, 4.4), TIME = c(0, 0.25, 0.57,
1.12, 2.02, 3.82, 0, 0.27, 0.52, 1, 1.92, 3.5), CONC = c(0.74,
2.84, 6.57, 10.5, 9.66, 8.58, 0, 1.72, 7.91, 8.31, 8.33,
6.85)), row.names = c(NA, -12L), class = c("tbl_df", "tbl",
"data.frame"))
>
附加信息: 我想我找到了问题所在。原始 tibble 上的 spec= info 包含与使用 read_csv 创建 tibble 的时间相关的信息。当 tibble 经历嵌套/非嵌套转换时,spec= info 已被丢弃。还有另一个线程提到 spec= 信息与 tibble 的内容不同步:Remove attributes from data read in readr::read_csv。在这种情况下,他们建议删除 spec= 属性:
attr(df, 'spec') <- NULL
【问题讨论】:
-
你能用
dput添加你的数据样本吗?基于复制上面的示例数据,我在运行时得到TRUE:identical(as_tibble(df), df %>% nest(data = c(TIME, CONC)) %>% unnest(cols = c(data)))。 -
我编辑了原始帖子以添加 dput。谢谢。