Question

Given is the data.frame "data" with one column containing numeric values. I now want to calculate the mean of the range from value 2 to value 10 only.

I tried to adress it like this

mean(data$pressure[2]:data$pressure[10])

but r just prints the single value of data$pressure[2], no mean in sight. The problem seems to be all about communicating to r, that the mean is supposed to cover a range, instead of a list.

As aimed-at example:

mean(1:4)

which calculates the mean of 1, 2, 3 and 4 perfectly (=2.5).

I would prefere to do this without using packages, just with the default functions of r.

Edit: By trying it with more numbers as input for the mean I noticed that the output is limited to six decimal places. Is there a way to increase the number of digits?

Was it helpful?

Solution

The correct syntax is:

mean(data$pressure[2:20])

or if you mean to get the mean value of all values between 2 and 10:

with(data, mean(pressure[pressure > 2 & pressure < 10]))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top