R数据用字符串填写NA

DATA = data.frame("ID"=c(1,1,1,2,2,2,3, 3),
"ANIM" = c('CAT','','','','FOX','','','DOG'))



DATAWANT = data.frame("ID"=c(1,1,1,2,2,2,3,3),
"ANIM" = c('CAT','CAT','CAT','FOX','FOX','FOX','DOG','DOG'))

我无法更改DATA以获得DATAWANT,在这里我尝试:

DATA[, ANIM := lapply(.SD, function(x) x[!is.na(x)][1]) , ID]
评论