-2

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
Dr.Data
  • 167
  • 1
  • 10
  • 2
    It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Images are not the right way to share data/code. You may use `df <- transform(df, Survey_score = ave(Response, EmployeeId))` to add `Survey_score` as new column. – Ronak Shah Sep 30 '21 at 02:54

1 Answers1

-1

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
Park
  • 14,771
  • 6
  • 10
  • 29