I have three parameters (3 columns)
x <- c(1, 1, 2, 2, 2, 2, 1, 1, 2)
y <- c(1, 1, 1, 2, 2, 2, 3, 3, 3)
and
z <- c(10, NA, 16, 25, 41, NA, 17, 53, 26)
I need for each y
calculate the mean of column z
, where x==1
How can I do it using the aggregate
function in R?
data <- data.frame(x=c(1, 1, 2, 2, 2, 2, 1, 1, 2),
y=c(1, 1, 1, 2, 2, 2, 3, 3, 3),
z=c(10, NA, 16, 25, 41, NA, 17, 53, 26))
data
x y z
1 1 1 10
2 1 1 NA
3 2 1 16
4 2 2 25
5 2 2 41
6 2 2 NA
7 1 3 17
8 1 3 53
9 2 3 26