[R] Question about a perceived irregularity in R syntax

Peter Dalgaard pdalgd at gmail.com
Fri Jul 23 00:13:29 CEST 2010


Pat Schmitz wrote:
> Both vector query's can select the values from the data.frame as written,
> however in the first form assigning a value to said selected numbers fails.
>  Can you explain the reason this fails?
> 
> dat <- data.frame(index = 1:10, Value = c(1:4, NA, 6, NA, 8:10))
> 
> dat$Value[dat$Value == "NA"] <- 1 #Why does this  fails to work,
> dat$Value[dat$Value %in% NA] <- 1 #While this does work?
> 
> 
> #Particularly when str() results in an equivalent class
> dat <- data.frame(index = 1:10, Value = c(1:4, NA, 6, NA, 8:10))
> str(dat$Value[dat$Value %in% NA])
> str(dat$Value[dat$Value == "NA"])

1. NA and "NA" are very different things
2. checkout is.na() and its help page



-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list