[R] Odp: NA-values and logical operation

Petr PIKAL petr.pikal at precheza.cz
Tue Jan 13 12:08:12 CET 2009


Hi


r-help-bounces at r-project.org napsal dne 13.01.2009 11:58:45:

> Dear list,
> 
> as a result of a logical operation I want to assign
> a new variable to a DF with NA-values.
> 
> z <- data.frame( x = c(5,6,5,NA,7,5,4,NA),
>                   y = c(1,2,2,2,2,2,2,2) )
> 
> p <- (z$x <= 5) & (z$y == 1)
> p
> z[p, "p1"] <-5
> z
> # ok, this works fine
> 
> z <- z[,-3]
> 
> p <- (z$x <= 5) & (z$y == 2)
> p
> z[p, "p2"] <-5
> z
> # this failed... - how can I assign the value '5' to the new
> # var "p2"

z[which(p), "p2"] <-5

works.

Regards
Petr


> 
> Thanks for any help!!
> Patrick
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list