packages - Combine rows/observations by column value in R -


i have data frame 3 columns:

 id        class     score  abc123    science   1  jkl456    math      0  zpe789    english   1  abc123    science   0  jkl456    math      0  yth293    art       1 

i want combine observations id, , add column sums scores (total score), shows number attempted (even if got wrong), , calculates percent correct, so:

 id        class     total score     number attempted      percent  abc123    science   1               2                      50  jkl456    math      0               2                       0  zpe789    english   1               1                     100  yth293    art       1               1                     100 

are there r packages or functions collapse across id , corresponding class , produce these results? thank you.

df <- read.table(textconnection("id        class     score  abc123    science   1  jkl456    math      0  zpe789    english   1  abc123    science   0  jkl456    math      0  yth293    art       1"), header = true) 

then do:

library(dplyr) df %>% group_by(id) %>%    summarise("total_score" = sum(score),             "number_attempted" = n(),             "percent" = (total_score/number_attempted)*100)  

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -