Home > Mobile >  Getting error in R when trying to calculate mean
Getting error in R when trying to calculate mean

Time:10-22

I am trying to calculate the mean of one of the variables in a data frame in R, and even though I have confirmed that the values are numeric I get the following error:

Warning in mean.default(x) : argument is not numeric or logical: returning NA

I have tried to calculate this in many different ways based on my research of different functions available, but I always end up with the same error. Here's my latest attempt:

x <- all_trips_cleaned %>% 
  select(ride_length)

mean_ride_length <- mean(x)

I am new to R so this is probably a simple fi- any advice is appreciated!

Thanks

CodePudding user response:

Instead of select, use pull to return a vector. select returns a data.frame/tibble with a single column and the mean works on a vector. From the 'x' dataset using OP's code, extract the 'ride_length' column with $ or [[ and get the mean

mean_ride_length <- mean(x$ride_length)

or use pull

library(dplyr)
x <- all_trips_cleaned %>% 
  pull(ride_length)

mean_ride_length <- mean(x)
  • Related