R: ifelse statement to create new variable equal to value of second variable conditional on value of third variable -


i'm trying create new variable (nphours) value of value of second variable (hours) conditioned on value of third variable (prod) being equal "n", otherwise want value of (nphours) "0".

sample data:

samp<-structure(list(hours = c(4.05, 4.05, 3.95, 3.95, 1), prod = structure(c(2l,  1l, 2l, 1l, 1l), .label = c("n ", "y "), class = "factor"), nphours = c("0",  "0", "0", "0", "0")), .names = c("hours", "prod", "nphours"), row.names = c(na,  5l), class = "data.frame") 

the following ifelse statement produces "0".

samp$nphours <- ifelse(samp$prod == "n", samp$hours, 0) 

can assist?

str(samp$prod)     

so...report blank after "n" ....

samp$nphours <- ifelse(samp$prod == "n ", samp$hours, 0) 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -