Hi all. From yesterday I was trying to find the conditional mean of the variable. In my case, I need two variables. One is continuous (positive), and the second one is binomial (yes=1, no=0). So, I have to find the mean of the first variable (continuous), if the second variable will equal to 1 (yes). And repeat the operation for the the same variable if the second variable is no (0). Also, I need to include na.rm = TRUE so that error didn't appear in my command line, because there are gaps in the table not filled in (NA). I have tried some commands, but they seem to be totally incorrect. Here are some of my attempts (mydata - data name, it was subsetted from the main data, because I needed only one year for all variables among all given years, x1 - continuous variable, x2 - binomial variable)
Part 1
if(mydata$x2 == 1) w <- mydata$x1
mean(w)
error: the condition has length > 1 and only the first element will be used
Part 2
mean(mydata[mydata$x2>0, "x1"])
Answer: [1] NA.
I don't know also how to integrate na.rm = TRUE argument here.
Please, help. Thanks.