let v be a vector 
> v <- c(1,2,4,99,6,999,7,8,99,5,2,4,9,2)
where the missing values where coded as 9, 99 or 999. 
Then the fastest way (i know) to recode them is:
> v[v %in% c(9,99,999)] <- NA
> v
[1]  1  2  4 NA  6 NA  7  8 NA  5  2  4 NA  2 
an example with matrices:
> m <- matrix(1:12, nrow=3, byrow=T,dimnames = list(letters[1:3],LETTERS[1:4]))
> m
A  B  C  D
a 1  2  3  4
b 5  6  7  8
c 9 10 11 12
> m[m %in% c(9,99,999)] <- NA
> m
A  B  C  D
a  1  2  3  4
b  5  6  7  8
c NA 10 11 12
This does not work on dataframes. You have to operate on each column separately. If you have many columns maybe it is worth a try to load the package epicalc or lordif and use the command recode() or you do it in loop ...
my personal notepad - for all the things I used to write on pieces of paper ( which where never seen again esp. when I needed them ) ...
Sunday, June 5, 2011
Subscribe to:
Post Comments
                                      (
                                      Atom
                                      )
                                    
 
 
No comments :
Post a Comment