I'm trying to work out a mean of a variable using rows that are equal to another value using:
pp$mmean[pp[,1] == '1'] <- mean(pp$mm)[1:nrow(pp[,1] == '1')]
That is I'm trying to work out the mean of mm - using rows where the first column == 1 (excluding every other row if it doesn't equal 1) where the pp$mmean result will only be indicated next to these rows. The above code gives me:
Error in 1:nrow(pp[, 1] == "1") : argument of length 0
I want to do this multiple times for every unique value in pp[,1]... and will set up a for loop for this.
Not sure what I'm doing wrong here...
Example of data, pp:
Plan X mm
1 95 0.323
1 275 0.341818
1 2 0.618
1 75 0.32
1 13 0.399
1 20 0.40
2 219 0.393
2 50 0.060
2 213 0.39
2 204 0.4961
2 19 0.393
2 201 0.388
etc...