Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Calculate the mean value of a variable by unique values of another variable in a dataframe?

Tags:

r

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.

like image 240
Richard Avatar asked Feb 17 '13 18:02

Richard


People also ask

How do I get unique values from a Dataframe in R?

To find unique values in a column in a data frame, use the unique() function in R. In Exploratory Data Analysis, the unique() function is crucial since it detects and eliminates duplicate values in the data.

How do I count unique values in a variable in R?

Method 1: Using length(unique()) function Unique() function when provided with a list will give out only the unique ones from it. Later length() function can calculate the frequency. Example 1: R.

How do you find the mean of multiple variables in R?

To find the mean of multiple columns based on multiple grouping columns in R data frame, we can use summarise_at function with mean function.


1 Answers

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
like image 160
Matthew Lundberg Avatar answered Nov 01 '22 16:11

Matthew Lundberg