0

I have a data frame with a list of IDs and other variables like so:

Student_ID       Score
6                94
2                63
6                100
7                44
6                97
2                67

I would like to create another data frame that consists of just the Student_ID and average score like this:

Student_ID       Avg_Score
2                65
6                97
7                44 

The actual data set is much larger, of course.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Justin D
  • 11
  • 2

1 Answers1

1

You can use dplyr package:

df %>% group_by(Student_ID) %>% summarise(Avg_Score = mean(Score))


# # A tibble: 3 x 2 
#   Student_ID Avg_Score 
#        <int>     <dbl> 
# 1          2        65 
# 2          6        97 
# 3          7        44

You can also use aggregate in base R:

aggregate( Score ~ Student_ID, df, mean) #column name will remain as "Score"

#   Student_ID Score 
# 1          2    65 
# 2          6    97 
# 3          7    44
M--
  • 25,431
  • 8
  • 61
  • 93