r - Mutate dplyr to select first value of a variable -
i have dataframe looks this
test<-data.frame(x=c(100,100,101,101,102,102),y=c(1,2,1,2,1,2),z=c(na,na,0.1,na,na,0.5)) i want group x. create new variable ynew @ z , select first value not na , set ynew=y. if both values in z na ynew na. ynew should na,na,1,1,2,2. trying using dplyr.
i stuck following
group_by(test,x) %>% mutate(ynew=ifelse(all(is.na(z)),na_integer_,y[corresponding index]))
the following dplyr approach works if test data.table
library(data.table) test <- data.table(test) test %>% group_by(x) %>% mutate(ynew = first(y[!is.na(z)])) # source: local data table [6 x 4] # x y z ynew # (dbl) (dbl) (dbl) (dbl) #1 100 1 na na #2 100 2 na na #3 101 1 0.1 1 #4 101 2 na 1 #5 102 1 na 2 #6 102 2 0.5 2 or can use data.table way
test[, ynew := y[!is.na(z)], x]
Comments
Post a Comment