3

I am trying to get a correlation between two variables at the end of a pipe operation, why don't these work?

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

thanks

user63230
  • 4,095
  • 21
  • 43

4 Answers4

5

Don't forget about %$% !

library(magrittr)

iris %$% 
  cor(Sepal.Length, Sepal.Width, use = "complete.obs")
#> [1] -0.1175698

Created on 2021-03-02 by the reprex package (v0.3.0)

tomasu
  • 1,388
  • 9
  • 11
4

map and map2 are for iteration - you don't want to iterate over anything, you just want to call cor on two columns. I'd suggest

iris %>%
  with(cor(Sepal.Length, Sepal.Width, use = "complete.obs"))
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
3

It doesn't need a map as this can be done within summarise

library(dplyr)
iris %>% 
  summarise(out = cor(Sepal.Length, Sepal.Width, use = "complete.obs"))
#       out
#1 -0.1175698

With map2, the tasks performed are applying a function on each corresponding element of the the concerned columns, where as the cor is on the full column taken as a single unit

The second option is doing similar one as

Map(cor, iris$Sepal.Length, iris$Sepal.Width,
        MoreArgs = list(use = 'complete.obs'))

iris %>% 
  dplyr::select(Sepal.Length, Sepal.Width) %>% 
  {map2(.$Sepal.Length, .$Sepal.Width, .f = cor, use = "complete.obs")}

Or with pmap

iris %>%
   dplyr::select(Sepal.Length, Sepal.Width) %>% 
   pmap(~ cor(.x, .y, use = "complete.obs"))

NOTE: all of them returns NA as the number of observations are 1

akrun
  • 874,273
  • 37
  • 540
  • 662
1

Thanks to @akrun I remembered that this may also work which it does, see here

iris %>%    
  {(cor(.$Sepal.Length, .$Sepal.Width, use = "complete.obs"))}
user63230
  • 4,095
  • 21
  • 43