[R] Rekeying value denoting NA

David Winsemius dwinsemius at comcast.net
Tue Nov 8 17:07:53 CET 2011


On Nov 8, 2011, at 8:57 AM, Jean V Adams wrote:

> SML wrote on 11/07/2011 09:10:30 PM:
>>
>> I'm trying to rekey values which denote there is no values, i.e.,
>> '-999' in a dataset which contains both '-999' and NA entries.
>> When I try the following command I get the following error:
>>
>>> data.frame[data.frame$MAR <= -99999,"MAR"] <- NA
>>
>> "missing values are not allowed in subscripted assignments of data
>> frames"
>>
>> Example of data:
>> YEAR       JAN     FEB     MAR     ...     DEC
>> 1931       5       -999    NA              3
>> 1932       2       1       -999            2
>> .
>> .
>> .
>> 2010       -999    NA      2               1
>>
>>
>> I've tried to replace the NAs with -999 values first to remove the NA
>> values, but got the same error.
>>
>> I'm quite new to R, and these little issues seem to be a stumbling
>> block. Many thanks for any help you might be able to offer.
>
>
> First of all, you should call your data frame something other than
> data.frame because data.frame is already a function in the base  
> package of
> R.  Let's call it df, instead,
>        df <- data.frame
>        rm(data.frame)
>
> Secondly, it looks like the variables in your data frame (JAN, FEB,  
> MAR,
> ..., DEC) are character not numeric, because their values are left  
> aligned
> in your example print out.  You can test this out by showing the  
> class of
> each variable in the data frame,
>        lapply(df, class)
>
> If the variables are character, you can convert them to numeric,
>        df2 <- as.data.frame(lapply(df, as.numeric))
>
> Then you can convert all the -999 values to NAs,
>        df2[df2 < -99] <- NA

Agreed this is what _should_ be done.
>
> YEAR JAN FEB MAR ... DEC
> 1931   5  NA  NA       3
> 1932   2  NA  NA       2
> .
> .
> .
> 2010  NA  NA   2       1

But ... I thought she wanted (unwisely in my opinion) to go the other  
way, NA's -> -999. In R the replacement of NA's is a bit convoluted  
because nothing "=="'s NA. You might need use the `is.na` function in  
this manner.

  df[is.na(df[["MAR"]]), "MAR"] <- -999

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list