There is a particular reoccurring problem that is very frustrating and I can't figure out the correct way to do this in dplyr.
How do you get dplyr to save a 'result' as a numeric value, and not a tibble data frame?
Code:
library(tidyverse)
library(nycflights13)
flights <- flights
a <- flights %>% filter(carrier == "AA") %>% length()
b <- flights %>% filter(carrier == "AA") %>% mean()
c <- flights %>% filter(carrier == "AA") %>% summarise(mean(hour))
These three lines represents the source of my frustration.
a. Why does 'a' work, i.e it returns a Value? (Bonus question, why is there the letter L next to 19 in the global environment?)
b. Why does this not work? It seems like it "should" if you compare it to the line above.
c. This kinda works in that I get the mean - but how do I get it as just a value (I want to use the value in other calculations, so I don't want a dataframe).
Obviously there is some nuance to dplyr that Iv'e not understood so far. Any help would be greatly appreciated!
SOLUTION:
mean <- flights %>% filter(carrier == "AA") %>% pull(hour) %>% mean()
pull() extracts a single column
L means intager
thanks all!