r - Count no-NA values per row -


family_id<-c(1,2,3) age_mother<-c(30,27,29) dob_child1<-c("1998-11-12","1999-12-12","1996-04-12")##child 1 birth day dob_child2<-c(na,"1997-09-09",na)##if no child,na dob_child3<-c(na,"1999-09-01","1996-09-09") dt<-data.table(family_id,age_mother,dob_child1,dob_child2,dob_child3) 

now have dt, how can use table know how many children each family have using syntax this:

dt[,apply..,keyby=family_id]##this code wrong 

this may work:

> dt$total_child <- as.vector(rowsums(!is.na(dt[, c("dob_child1",  "dob_child2", "dob_child3")]))) > dt   family_id age_mother dob_child1 dob_child2 dob_child3 total_child 1         1         30 1998-11-12       <na>       <na>           1 2         2         27 1999-12-12 1997-09-09 1999-09-01           3 3         3         29 1996-04-12       <na> 1996-09-09           2 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -