gpt4 book ai didi

R bind_rows() 错误 : Argument 1 must have names

转载 作者:行者123 更新时间:2023-12-04 01:45:51 28 4
gpt4 key购买 nike

我有 3 个数据框。每个 df 有 3 个 A 变量和 3 个 B 变量和 3 个阈值 (1, 2, 3)

df1 <- tibble(
var1A= rnorm(1:10) +1,
var1B= rnorm(1:10) +1,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)


df2 <- tibble(
var1A= rnorm(1:10) +1,
var1B= rnorm(1:10) +1,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)


df3 <- tibble(
var1A= rnorm(1:10) +1,
var1B= rnorm(1:10) +1,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)

thresholds = c(1, 2, 3)
list_dfs = c('df1','df2','df3')

现在我想对所有 A 和 B 变量以及所有 df 执行 t.test(例如 t.test df1$var1A 和 df1$var1B)。我将在 matrixTests::col_t_welch()

的帮助下执行 t.test
map(list_dfs,
function(df_name){
x <- get(df_name)
lapply(thresholds, function(i){

col_t_welch(x %>%
pull(paste0("var",i,"A")),
x %>%
pull(paste0("var",i,"B")))

})
})

现在我为每个阈值和每个 df 生成了一个 varA 和 varB 的 t.test 表。

最后我想用 bind_rows() 绑定(bind)所有这些表。但随后我收到一条错误消息:Argument 1 must have names

map(list_dfs,
function(df_name){
x <- get(df_name)
lapply(thresholds, function(i){

col_t_welch(x %>%
pull(paste0("var",i,"A")),
x %>%
pull(paste0("var",i,"B")))

})
}) %>% bind_rows

Error: Argument 1 must have names

谁能帮我解决这个问题?

最佳答案

这是你想要的结果吗?

首先,我根据您的问题加载库并创建数据。

library(tibble)
library(purrr)
library(dplyr)
library(matrixTests)

df1 <- tibble(
var1A= rnorm(1:10) +1,
var1B= rnorm(1:10) +1,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)


df2 <- tibble(
var1A= rnorm(1:10) +1,
var1B= rnorm(1:10) +1,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)


df3 <- tibble(
var1A= rnorm(1:10) +1,
var1B= rnorm(1:10) +1,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)

thresholds = c(1, 2, 3)
list_dfs = c('df1','df2','df3')

在这里,我在绑定(bind)之前取消列出结果。

map(list_dfs,
function(df_name){
x <- get(df_name)
lapply(thresholds, function(i){

col_t_welch(x %>%
pull(paste0("var",i,"A")),
x %>%
pull(paste0("var",i,"B")))

})
}) %>%
unlist(recursive = FALSE) %>%
bind_rows()

这给出了,

#>   obs.x obs.y obs.tot    mean.x    mean.y   mean.diff     var.x     var.y
#> 1 10 10 20 0.4123358 0.9386079 -0.52627205 1.2887733 1.4188697
#> 2 10 10 20 1.4848642 1.8852731 -0.40040891 0.7594906 1.9971866
#> 3 10 10 20 2.9905342 3.1454473 -0.15491307 0.9501264 0.6863846
#> 4 10 10 20 1.2409187 0.9453490 0.29556964 1.8969049 0.5213807
#> 5 10 10 20 2.0823664 2.3150223 -0.23265591 0.5171046 0.6771720
#> 6 10 10 20 3.0354769 2.2958400 0.73963696 0.8915344 1.1509940
#> 7 10 10 20 0.5546491 0.8868825 -0.33223340 0.6404670 0.4313640
#> 8 10 10 20 2.9031533 2.5956085 0.30754479 1.1602239 1.6080605
#> 9 10 10 20 3.1435888 3.1988889 -0.05530018 1.7926813 0.4374122
#> stderr df statistic pvalue conf.low conf.high alternative
#> 1 0.5203502 17.95854 -1.0113806 0.3252679 -1.6196681 0.5671240 two.sided
#> 2 0.5250407 14.98023 -0.7626246 0.4575287 -1.5196353 0.7188175 two.sided
#> 3 0.4045381 17.54432 -0.3829381 0.7063657 -1.0064012 0.6965751 two.sided
#> 4 0.4917607 13.59994 0.6010437 0.5576963 -0.7620703 1.3532096 two.sided
#> 5 0.3455831 17.68236 -0.6732272 0.5095070 -0.9596350 0.4943232 two.sided
#> 6 0.4519434 17.71416 1.6365699 0.1193625 -0.2109603 1.6902343 two.sided
#> 7 0.3273883 17.34004 -1.0147992 0.3241530 -1.0219320 0.3574652 two.sided
#> 8 0.5261449 17.54094 0.5845249 0.5663102 -0.7999219 1.4150114 two.sided
#> 9 0.4722387 13.14519 -0.1171022 0.9085494 -1.0743655 0.9637651 two.sided
#> mean.null conf.level
#> 1 0 0.95
#> 2 0 0.95
#> 3 0 0.95
#> 4 0 0.95
#> 5 0 0.95
#> 6 0 0.95
#> 7 0 0.95
#> 8 0 0.95
#> 9 0 0.95

reprex package 创建于 2019-03-21 (v0.2.1)

关于R bind_rows() 错误 : Argument 1 must have names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55278386/

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