Calculate IQR for selected observations

53 views Asked by At

I have the following data and I would like to calculate IQR for only for those whose sex is equal to 1.

enter image description here

I have tried

if(Agogo$sex_2015==2) { IQR(Agogo$bmi) }

Is there any way to do this using ifelse or any other condition?

1

There are 1 answers

0
Fatih Aslan On

I'm assuming you want the get IQR of bmi

library(dplyr)
bmi<- sample(100,10)
edu_mo<-sample(50,10)
sex<-sample(2,10,replace= TRUE)

n<-data.frame(bmi,edu_mo,sex)

n %>% 
  filter(sex == 1) %>% 
  summarize(IQR = IQR(bmi))