r-当将函数应用于数据帧时,错误为该列是最后一个对象,并且tibble中的所有列都必须是向量



我正在尝试运行函数

growth<- function(x){
cor.test(df3$H1, x, method = "spearman", exact = FALSE)
}
v1<-map_dfr(df3, growth)

其中CCD_ 1示出全部是数字的。然而,当我运行这个时,它显示

错误:tibble中的所有列都必须是向量
x列H1是一个htest对象
x列laz_12是一个htest对象
x列waz_12是一个htest对象
x列hcz_12是一个htest对象
x列str(df3)0是一个htest对象
x。。。还有4个问题。

要使map_dfr工作,函数的输出必须是数据帧或tibble。growth函数的输出是最重要的对象。也许你可以tidy的输出返回一个tibble。

下面是一个mtcars数据集的示例。

growth<- function(x){ 
broom::tidy(cor.test(mtcars$mpg, x, method = "spearman", exact = FALSE)) 
}
v1 <- purrr::map_dfr(mtcars, growth, .id = "col")
v1
#   col   estimate statistic  p.value method                        alternative
#   <chr>    <dbl>     <dbl>    <dbl> <chr>                           <chr>      
# 1 mpg      1            0  0        Spearman's rank correlation rho two.sided  
# 2 cyl     -0.911    10425. 4.69e-13 Spearman's rank correlation rho two.sided  
# 3 disp    -0.909    10415. 6.37e-13 Spearman's rank correlation rho two.sided  
# 4 hp      -0.895    10337. 5.09e-12 Spearman's rank correlation rho two.sided  
# 5 drat     0.651     1902. 5.38e- 5 Spearman's rank correlation rho two.sided  
# 6 wt      -0.886    10292. 1.49e-11 Spearman's rank correlation rho two.sided  
# 7 qsec     0.467     2908. 7.06e- 3 Spearman's rank correlation rho two.sided  
# 8 vs       0.707     1601. 6.19e- 6 Spearman's rank correlation rho two.sided  
# 9 am       0.562     2390. 8.16e- 4 Spearman's rank correlation rho two.sided  
#10 gear     0.543     2495. 1.33e- 3 Spearman's rank correlation rho two.sided  
#11 carb    -0.657     9043. 4.34e- 5 Spearman's rank correlation rho two.sided  

相关内容

最新更新