gpt4 book ai didi

r - 如何取消 R 中测试产生的数据帧的嵌套

转载 作者:行者123 更新时间:2023-12-01 22:47:26 27 4
gpt4 key购买 nike

我想取消嵌套 res2数据帧,但出现以下错误:x[[1]] 必须是向量,而不是一个物体。我该怎么办?

library(tidyverse)

df <- tibble(id = c(1,2,3,4,5,6,7,8),
test = c(2,1,2,1,2,1,2,1),
difference = c(1,3,4,7,3,9,3,7))

shap <- function(t) {
df %>% filter(test == t)
return(shapiro.test(df$difference))
}

test <- 1:2

res <- map(test, function(x) shap(x))
res2 <- enframe(res)
res3 <- unnest(res2, cols = c(value))
#> Error in `list_sizes()`:
#> ! `x[[1]]` must be a vector, not a <htest> object.

#> Backtrace:
#> ▆
#> 1. ├─tidyr::unnest(res2, cols = c(value))
#> 2. ├─tidyr:::unnest.data.frame(res2, cols = c(value))
#> 3. │ └─tidyr::unchop(data, any_of(cols), keep_empty = keep_empty, ptype = ptype)
#> 4. │ └─tidyr:::df_unchop(cols, ptype = ptype, keep_empty = keep_empty)
#> 5. │ └─tidyr:::list_init_empty(x = col, null = TRUE, typed = keep_empty)
#> 6. │ └─vctrs::list_sizes(x)
#> 7. └─vctrs:::stop_scalar_type(`<fn>`(`<htest>`), "x[[1]]", `<env>`)
#> 8. └─vctrs:::stop_vctrs(...)
#> 9. └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))

最佳答案

您需要从每个测试结果中提取什么?如果您查看 res2$value[[1]],您会发现有一些可以使用的统计信息:

dput(res2$value[[1]])
# structure(list(statistic = c(W = 0.907866418594146), p.value = 0.339283842099497,
# method = "Shapiro-Wilk normality test", data.name = "df$difference"), class = "htest")

有了这个,我们添加 $statistic$p.value:

res2 %>%
bind_cols(map_dfr(res2$value, ~ .[c("statistic", "p.value")]))
# # A tibble: 2 × 4
# name value statistic p.value
# <int> <list> <dbl> <dbl>
# 1 1 <htest> 0.908 0.339
# 2 2 <htest> 0.908 0.339

关于r - 如何取消 R 中测试产生的数据帧的嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74997753/

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