Bubbles
Bubbles

Reputation: 5

Extract values within mean+1SD and mean-1SD in R

I would like to extract values falling within mean-1SD and mean+1SD, mean-2SD and mean+2SD from a data frame 'data' and in sum the values from extractions should be equal to the final sample size, but my following code do not give me correct values.

Any suggestions?

within1sd <- data[!(data$values < mean-SD & data$values > mean ), ]


within1sd <- data[!(data$values < mean-SD & data$values > mean ), ]

Upvotes: 0

Views: 532

Answers (1)

Sam Mason
Sam Mason

Reputation: 16194

as @akrun says, you need to call/evaluate the mean and sd functions to get the mean and standard deviation, then you can use them to get the values you're after

something like:

mu <- mean(data$values)
sigma <- sd(data$values)
data[abs(data$values - mu) < sigma,]

you could of course put all this on one line, but thought this was easier to read

Upvotes: 3

Related Questions