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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With