Tomas R
Tomas R

Reputation: 556

Can I get dplyr output as value, and not as data frame?

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!

Upvotes: 1

Views: 1737

Answers (1)

Samuel Calderon
Samuel Calderon

Reputation: 741

a. length() returns the number of elements in an object. When the object is a data.frame (or tibble) it will return the number of columns. The L after the number means it is an integer.

b. mean() requieres a numeric vector to work. In b you are passing a tibble to the function.

c. dplyr functions are meant to receive tibbles for input and produce tibbles for output. You can pull() a column of your tibble so it becomes a vector.

C <- flights %>% filter(carrier == "AA") %>% pull(hour) %>% mean()

Upvotes: 1

Related Questions