I'd like to create a new column (Survey_score) to this dataset that calculates the average of Question (Q1-Q4) for each ID. I suspect I need to loop through each ID, but I'm sorta new to R. Any tips?
| ID | Question | Answer |
|---|---|---|
| 1 | Q1 | 2 |
| 1 | Q2 | 2 |
| 1 | Q3 | 1 |
| 1 | Q4 | 4 |
| 2 | Q1 | 1 |
| 2 | Q2 | 2 |
| 2 | Q3 | 4 |
| 2 | Q4 | 2 |
CodePudding user response:
By using dplyr(I let Id as 1, 2, 3)
library(dplyr)
df %>%
group_by(EmployeeId) %>%
summarize(n = mean(Response))
EmployeeId n
<dbl> <dbl>
1 1 3.25
2 2 3.75
3 3 2.75
