[R] NAs in indices
Muenchen, Robert A (Bob)
muenchen at utk.edu
Sun Sep 2 16:20:26 CEST 2007
Hi All,
I'm fiddling with an program to read a text file containing periods that
SAS uses for missing values. I know that if I had the original SAS data
set instead of a text file, R would handle this conversion for me.
Data frames do not allow missing values in their indices but vectors do.
Why is that? A search of the error message points out the problem and
solution but not why they differ. A simplified program that demonstrates
the issue is below.
Thanks,
Bob
# Here's a data frame that has both periods and NAs.
# I want sex to remain character for now.
sex=c("m","f",".",NA)
x=c(1,2,3,NA)
myDF <- data.frame(sex,x,stringsAsFactors=F)
rm(sex,x)
myDF
# Substituting NA into data frame does not work
# due to NAs in the indices. The error message is:
# missing values are not allowed in subscripted assignments of data
frames
myDF[ myDF$sex==".", "sex" ] <- NA
myDF
# This works because myDF$sex is a vector and vectors allow NAs in
indexes.
# Why don't data frames allow this?
myDF$sex[ myDF$sex=="." ] <- NA
myDF
=========================================================
Bob Muenchen (pronounced Min'-chen), Manager
Statistical Consulting Center
U of TN Office of Information Technology
200 Stokely Management Center, Knoxville, TN 37996-0520
Voice: (865) 974-5230
FAX: (865) 974-4810
Email: muenchen at utk.edu
Web: http://oit.utk.edu/scc,
News: http://listserv.utk.edu/archives/statnews.html
More information about the R-help
mailing list