我试图在管道操作结束时获得两个变量之间的相关性,为什么这些不起作用?
library(tidyverse)
iris %>%
map(~cor(.$Sepal.Length, .$Sepal.Width, use = "complete.obs"))
#or
iris %>%
dplyr::select(Sepal.Length, Sepal.Width) %>%
map2(~cor(.x, .y, use = "complete.obs"))
Run Code Online (Sandbox Code Playgroud)
谢谢
别忘了%$%!
library(magrittr)
iris %$%
cor(Sepal.Length, Sepal.Width, use = "complete.obs")
#> [1] -0.1175698
Run Code Online (Sandbox Code Playgroud)
由reprex 包于 2021 年 3 月 2 日创建(v0.3.0)
map和map2用于迭代 - 您不想迭代任何内容,您只想调用cor两列。我建议
iris %>%
with(cor(Sepal.Length, Sepal.Width, use = "complete.obs"))
Run Code Online (Sandbox Code Playgroud)