user1913921
user1913921

Reputation: 299

How to take the mean of values between a range of rows in R?

This is a follow-up question to a question that was successfully answered for me on this site last year see How to take the mean of last 10 values in a column before a missing values using R.

Previously, I had asked how to take the average of the last 10 rows in a column before a missing value. My data are growth rates for trees - my first column is year and the columns to the right are growth rates for individual trees, ending in the year each tree died. Before each tree was "born" and after the tree died, the growth rate values are NA. When I needed to take the average of the last 10 years before each tree died, the code which worked was:

sapply(dataframe, function(dataframe) mean(tail(na.omit(dataframe), 10)))

Later, I needed to take the average growth rate for the first 50 years of each tree's life, so I did this by substituting "head" for "tail", using this code:

sapply(dataframe, function(dataframe) mean(head(na.omit(dataframe), 50)))

Now, my question is: how would I take the mean of each tree' growth between the first 50 and 100 years of growth? So, I want the average growth for each tree between the time it was 50-100 years old? Is there a function I am not aware of (similar to "head" or "tail") that would allow me take the average between specific rows? The difficulty is that each tree was born (or died) in a different year, so I can't simply index certain years.

Thanks for any help you can provide.

Katie

Upvotes: 0

Views: 4510

Answers (1)

mrip
mrip

Reputation: 15163

Combining head and tail can get you what you need. For example:

> x<-1:10
> tail(head(x,4),2)
[1] 3 4

So in your case it looks like what you want is:

sapply(dataframe, function(x) mean(tail(head(na.omit(x), 100),50)))

You could also use explicit indexing if you want, since na.omit will drop all the NA values, so na.omit(x)[51:100] will be the years 51-100 since a tree is born. So you could also go with

sapply(dataframe, function(x) mean(na.omit(x)[51:100]))

The only difference between the two is what happens if a column has less than 100 valid entries. In this situation, the head/tail method will give you the average of the last 50 valid entries (or less, if there are less than 50 total), whereas the 51:100 method will return NA.

Upvotes: 0

Related Questions