[R] NAs in indices
Jim Lemon
jim at bitwrit.com.au
Mon Sep 3 12:37:48 CEST 2007
Muenchen, Robert A (Bob) wrote:
> 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
>
Hi Bob,
What happens is that you don't get FALSE when you ask if something==NA,
you get NA. However, if you use the "which" function, it cleans up the
NAs for you and the result of that should do what you want.
myDF[which(myDF$sex=="."),"sex"]<-NA
Jim
More information about the R-help
mailing list