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"]) Thanks Pat -- Patrick Schmitz Graduate Student Plant Biology 1206 West Gregory Drive RM 1500 [[alternative HTML version deleted]]
Peter Dalgaard
2010-Jul-22 22:13 UTC
[R] Question about a perceived irregularity in R syntax
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
Reasonably Related Threads
- Label individual points in lattice by and ID
- xyplot, overlay two variables on one plot with group factors
- Trouble variable scoping a function writing with get()
- Applying Logical statement to DateTime string as factor
- Can You Recommend Books for Linear Mixed Models in R