Friday, April 1, 2011

R: recode levels (missing value) to NA

R load dataset from spss and assign value to missing value. Also in my original dataset, they have coded missing value with a meaningful -8 -9. I hate it! After marathon search in online forum of R topic, I digged out my way to recode levels. Here are the solutions:


>attach(dataset)
> summary(variable)
>table(variable)
>table(as.numeric(variable))
>levels(variable)  ##if that s a numeric factor, it will have error message. command above just to have idea of how many NA and what value of level to recode. 


>levels(variable) [level value assigned] <- NA


#to check
>sum(is.na(variable))
# it tells you how many are missing, or
>table(as.numeric(variable)) 
#or
> levels(variable)
## NA level should appear then.





missing value for numeric variables:

> sum(is.na(BYSES2))
[1] 0
> BYSES2[BYSES2==-8] <- NA
> sum(is.na(BYSES2))
[1] 305
> BYSES2[BYSES2==-4] <- NA
> sum(is.na(BYSES2))
[1] 953
> describe(BYSES2)







Note: if type:
> variable[1:4] <- NA ## it means assign NA to the 1st to 4th row under this column(variable)
> variable[3] <- NA ## Likewise, assign NA to the 3rd row under the variable


It is little bit confusing but finally made it!