Horje
conditional mean statement r Code Example
[Solved] - conditional mean statement r Code Example
Try following codes
conditional mean statement r
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
Date: 2022-05-21 05:02:10
Category: r
More for You