bvowe
bvowe

Reputation: 3384

R Data.Table Copy First Value For Group

data=data.frame("StudentID"=c(1,1,1,2,2,3,3,3),
"Grade"=c(1,2,3,1,2,1,2,3),
"Score"=c(7,5,3,4,NA,8,9,7),
"ScoreWANT"=c(7,7,7,4,0,8,8,8))

I have 'data' shown above without 'ScoreWANT' which is what I hope to generate. 'ScoreWANT' takes the first 'Score' for each "StudentID', the 'Score' at 'Grade' = 1, and fills it down for the rest of the values for each 'StudentID'. There are no missing values at 'Grade' = 1. Any 'NA' values can be replaced with '0'.

Upvotes: 1

Views: 431

Answers (1)

akrun
akrun

Reputation: 887108

We can get the first non-NA 'Score' for each 'StudentID'

library(data.table)
setDT(data)[, ScoreWANT := first(Score[!is.na(Score)]), StudentID]

Or if we need to replace NA with 0 and then apply first

setDT(data)[, ScoreWANT := first(replace(Score, is.na(Score), 0)), StudentID]

If we want to replace only where there are no NA's in the 'Score' to first

setDT(data)[, ScoreWANT := 0][!is.na(Score), ScoreWANT := first(Score), StudentID]

Upvotes: 2

Related Questions