R: Calculate the mean value of a variable by unique values of another variable in a dataframe?
r
Solution
One of very many ways to do this:
x <- read.table(header=T, text="Pupil.ID State GPA
1 FL 3.9
2 TX 3.2
3 NY 2.2
4 AK 3.0
5 CO 2.4")
aggregate(GPA~State, data=x, FUN=function(x) c(mean=mean(x), count=length(x)))
## State GPA.mean GPA.count
## 1 AK 3.0 1.0
## 2 CO 2.4 1.0
## 3 FL 3.9 1.0
## 4 NY 2.2 1.0
## 5 TX 3.2 1.0
Problem
I am new to R. I have a data frame that looks like this: ``` Pupil ID State GPA 1 FL 3.9 2 TX 3.2 3 NY 2.2 4 AK 3.0 5 CO 2.4 ``` ... etc. What I would like to do is create a new data frame that looks like this: ``` State Mean GPA Number of pupils AL 2.91 23 AK 3.23 24 ``` and so on. In other words, I'd like to find the unique values for state, and calculate the mean GPA for each one and number of pupils for each one. Is this possible in R? I know I can do `table(data$State)` to get the unique states and number of pupils, but I don't know how to calculate the mean for the unique values of state.