Question about using the mean() function in a piped workflow

The mean() function in this piped workflow throws an error when I run it. The argument is a column of my data set. Here is the code:
hotel_bookings %>% mean(lead_time)

The error says "argument is not numeric or logical: returning NA", but all the values are numeric

The mean() function is not designed to be used with a pipe. You are passing the entire data frame to mean() as the object of which to take the mean. To get the mean of the lead_time, use mean(hotel_bookings$lead_time). Here is a simple example

DF <- data.frame(A = c(2,3,2), b = c(4,7,2))
DF |> mean(A)
#> Warning in mean.default(DF, A): argument is not numeric or logical: returning
#> NA
#> [1] NA
mean(DF$A)
#> [1] 2.333333

Created on 2025-05-03 with reprex v2.1.1

1 Like

perhaps you meant to use the exposition pipe

hotel_bookings %$%  mean(lead_time)

hotel_bookings$lead_time %>% mean()
would also work, or
hotel_bookings %>% pull(lead_time) %>% mean()
or
mean(hotel_bookings$lead_time)

2 Likes

Or use the with() function from base R:

hotel_bookings %>% with(mean(lead_time))

1 Like

Or yet another option: extract the column with pull:

 hotel_bookings %>% pull(lead_time) %>% mean()
1 Like

The with() function lets you easily use multiple variables from the piped data:

mtcars |> with(mean(disp))
#> [1] 230.7219

mtcars |> with(mean(disp/cyl))
#> [1] 35.02865

Created on 2025-05-06 with reprex v2.1.1

1 Like

Thank you all! You make the community great.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.