Why you're getting errors
mydata$x2
is a vector, and you can't use it for comparison this way.- Try with
mean(mydata[mydata$x2>0, "x1"], na.rm = TRUE)
Alternative
Use the by
function.
Let me illustrate by an example:
dataset <- data.frame(continuous = rnorm(n = 10),
binary = sample(x = 0:1, size = 10, replace = TRUE))
dataset
#> continuous binary
#> 1 -0.01978487 0
#> 2 -1.14185292 0
#> 3 0.20931787 0
#> 4 -0.63720730 0
#> 5 1.07750407 1
#> 6 -1.59274225 0
#> 7 -0.48722740 1
#> 8 -0.64151044 0
#> 9 -0.64111755 0
#> 10 0.99598287 1
# your method
mean(dataset[dataset$binary == 1, 1])
#> [1] 0.5287532
mean(dataset[dataset$binary == 0, 1])
#> [1] -0.6378425
# using by
by(data = dataset$continuous, INDICES = dataset$binary, FUN = mean)
#> dataset$binary: 0
#> [1] -0.6378425
#> --------------------------------------------------------
#> dataset$binary: 1
#> [1] 0.5287532
Created on 2019-03-19 by the reprex package (v0.2.1)
Hope this helps.
PS
Please ask your future questions in form of a reproducible example. In this case, it was not too difficult to understand what can be going wrong, but more than often it's not the case. You can go through this great post to know how to make a reprex: