gpt4 book ai didi

R nest/unnest of dataframe 导致不相同的对象

转载 作者:行者123 更新时间:2023-12-05 06:59:26 25 4
gpt4 key购买 nike

我第一次在 R 中使用 nest/unnest 函数,我不明白结果。我嵌套并立即取消嵌套并比较之前/之后的数据帧。为什么数据帧不相同?

> 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"))
>

附加信息:我想我发现了问题。原始小标题上的 spec= 信息包含与使用 read_csv 创建小标题时相关的信息。当 tibble 通过 nest/unnest 转换时,spec= 信息已被丢弃。还有另一个线程提到 spec= info 与 tibble 的内容不同步:Remove attributes from data read in readr::read_csv .在这种情况下,他们建议删除 spec= 属性:

attr(df, 'spec') <- NULL

最佳答案

根据我能够找到的内容,您的原始数据框与输出不相同的原因是原始数据框属于 col_spec 类,而输出不是。

使用新的 waldo 包,tidyverse 的一部分,我运行了以下命令:

compare(df, df %>% nest(data = c(TIME, CONC)) %>% unnest(cols = c(data)))
`attr(old, 'spec')` is an S3 object of class <col_spec>
`attr(new, 'spec')` is absent

看起来您使用 readr 读取了数据,并且生成的 df 是类 col_spec 的对象。嵌套原始 df 会删除此属性。

attr(df %>% nest(data = c(TIME, CONC)), 'spec')
NULL

因此,当您unnest 时,df 不相同。

关于R nest/unnest of dataframe 导致不相同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64416469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com