How to calculate the mean in R excluding NA values

Viewed 280

I am a newbie in R, so this is probably a simple question. I am working with a large data frame to find the average review score(1-5) of items if a service is being used. A lot of items have "NoReview" in the review column. Is there a way I can exclude the items that say "NoReview"? I tried using na.rm = TRUE but I am pretty sure it is only for data that says NA.

Attached link is the code I tried and the error I received.

1 Answers

You need to transform you review column in numeric

You can achieve that transforming "NoReview" values to NA and then the column to numeric.

Try this:

odat %>%
        mutate(review = case_when(
                review == "NoReview" ~ NA,
                TRUE ~ review_column)) %>%
        group_by(if_cainiao) %>%
        summarise(avgReview = mean(as.numeric(review), na.rm = T))
Related