Home > OS >  How to create a value not a list
How to create a value not a list

Time:05-01

I have this code:

m = sapply(split(DB, DB$ID), function(grp){
grp[max(grp$Time, na.rm =T), "Score"]-
grp[min(grp$Time, na.rm=T), "Score"]
})

Basically a code to calculate difference in score variables between rows (I have long data of repeated measures). I want to get the m as a value not as a list because I want to merge it back in the database how can I do this?

CodePudding user response:

Use a group_by operation

library(dplyr)
DB %>%
    group_by(ID) %>%
    summarise(Score2 = Score[max(Time, na.rm = TRUE)] - 
       Score[min(Time, na.rm = TRUE)], .groups = 'drop')
  •  Tags:  
  • r
  • Related