ebb
ebb

Reputation: 274

R -- mean function of column section

I am trying to include a mean calculation as part of a larger code. The idea is to calculate the mean from a series of values within a column, but not all the column.

For example, from column_x (10 entries) in yFile, calculate the mean of the last 4 values:

column_x
1
5
8
3
0
3
3
7
9
9

Result = 7

This is what I've got:

avg_subx <- mean(yFile$column_x, 7:10, trim = 0, na.rm = FALSE)

But for some reason, the result I am getting back is not the correct value. Could you help me finding out where I'm going wrong?

Thanks!

Upvotes: 1

Views: 559

Answers (1)

have you tried with tail function? With tail you can select the last n values of a data frame or a vector.

example:

avg_subx <- mean(tail(yFile$column_x,4))

In this case you're selecting the las 4 values.

Hope this can help you!

Upvotes: 2

Related Questions