Help about conditional mean


I have a project about rental apartments statistics. My problem is , i am trying to find the mean , sd or etc. of buildings which has only 1 room. But i cannot do it it gives me all the list's mean. What code should i put in, i am newbie in R. Thanks for your help.

Here is an example for you using the builtin iris dataset

library(tidyverse)

# mean of all Petal.Length (samples from 3 species mixed together)
mean(iris$Petal.Length)

# unique species are
unique(iris$Species)

# mean for filter for species setosa
mean(filter(iris, Species == "setosa")$Petal.Length)

# same again , is this  nicer syntax ?---

# mean of all Petal.Length (samples from 3 species mixed together)
mean(iris %>% pull(Petal.Length))

# unique species are
unique(iris %>% pull(Species))

# mean for filter for species setosa
mean(iris %>% 
       filter(Species == "setosa") %>% 
       pull(Petal.Length))

This didn't work for my problem. I tried all of it but it still gives me error. Does anyone know the solution?

can you be specific about your error ?

The sample code using the iris data set runs without error and shows how to calculate the mean of a continuous variable for all levels of a categorical variable compared to the mean for a subset of just one level of that categorical variable.

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