Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mean by factor by level

Tags:

Maybe this is simple but I can't find answer on web. I have problem with mean calculation by factors by level. My data looks typicaly:

factor, value a,1 a,2 b,1 b,1 b,1 c,1 

I want to get vector A contains mean only for level "a" If I type A on consol I want to get 1.5 And this method for calculating mean, must use factors.

Thank you in advance for help.

like image 938
Bartek Taciak Avatar asked Apr 30 '14 18:04

Bartek Taciak


People also ask

What is a factor level?

Factors are the variables that experimenters control during an experiment in order to determine their effect on the response variable. A factor can take on only a small number of values, which are known as factor levels.

How do you find factors and levels?

The number of levels of a factor or independent variable is equal to the number of variations of that factor that were used in the experiment. If an experiment compared the drug dosages 50 mg, 100 mg, and 150 mg, then the factor "drug dosage" would have three levels: 50 mg, 100 mg, and 150 mg.

How do I find the mean of a factor?

Hint: To find the mean of all factors of 10, we will write down the factors of 10. Factors of 10 are 1,2,5,10. We can use the formula Mean=Sum of the termsNumber of terms to find the mean.

What are factor levels in R?

Factors in R. Factors are data structures in R that store categorical data. They have a levels attribute that holds all the possible values that elements of the factor can take. R factors can be of any type. They only allow values permitted by the levels.


2 Answers

take a look at tapply, which lets you break up a vector according to a factor(s) and apply a function to each subset

> dat<-data.frame(factor=sample(c("a","b","c"), 10, T), value=rnorm(10)) > r1<-with(dat, tapply(value, factor, mean)) > r1          a          b          c  0.3877001 -0.4079463 -1.0837449 > r1[["a"]] [1] 0.3877001 

You can access your results using r1[["a"]] etc.

Alternatively, one of the popular R packages (plyr) has very nice ways of doing this.

> library(plyr) > r2<-ddply(dat, .(factor), summarize, mean=mean(value)) > r2   factor       mean 1      a  0.3877001 2      b -0.4079463 3      c -1.0837449 > subset(r2,factor=="a",select="mean")        mean 1 0.3877001 

You can also use dlply instead (which takes a dataframe and returns a list instead)

> dlply(dat, .(factor), summarize, mean=mean(value))$a        mean 1 0.3877001 
like image 59
JPC Avatar answered Oct 14 '22 05:10

JPC


The following code asks for the mean of value when factor = a:

mean(data$value[data$factor == "a"]) 
like image 33
Lenatis Avatar answered Oct 14 '22 06:10

Lenatis