Reputation: 3021
I have a dataset where inspection of the data shows some of the following, all of which should be missing
'missing'
'unknown'
'uncoded'
Am I correct in thinking that I can just replace all occurrences of these with "NA" ? Is this the preferred way of doing it ?
var[var=='missing'] <- NA
var[var=='unknown'] <- NA
var[var=='uncoded'] <- NA
Upvotes: 2
Views: 2298
Reputation: 174788
What you show is feasible, but you can simplify your code to a single call doing the comparison via the %in%
binary operator. Here is an example using some dummy data:
set.seed(1)
var <- factor(sample(c("missing","unknown","uncoded", 1:4), 100, replace = TRUE))
This gives us a factor vector like this:
> head(var)
[1] unknown uncoded 2 4 unknown 4
Levels: 1 2 3 4 missing uncoded unknown
> table(var)
var
1 2 3 4 missing uncoded unknown
14 15 17 13 10 18 13
To set all those values coded as any of c("missing","unknown","uncoded")
to NA
, we do it in a single shot:
var2 <- var ## copy for demo purposes, but you can over write if you wish
var2[var2 %in% c("missing","unknown","uncoded")] <- NA
which gives
> var2[var2 %in% c("missing","unknown","uncoded")] <- NA
> head(var2)
[1] <NA> <NA> 2 4 <NA> 4
Levels: 1 2 3 4 missing uncoded unknown
> table(var2)
var2
1 2 3 4 missing uncoded unknown
14 15 17 13 0 0 0
Notice how the original levels are preserved. If you want to remove those levels then we can apply the droplevels()
function to var2
:
var2 <- droplevels(var2)
which gives
> head(var2)
[1] <NA> <NA> 2 4 <NA> 4
Levels: 1 2 3 4
> table(var2)
var2
1 2 3 4
14 15 17 13
Also note that by default the NA
are not shown in the tabular output, but we rectify that to show you that they are still there:
> table(var2, useNA = "ifany")
var2
1 2 3 4 <NA>
14 15 17 13 41
Upvotes: 6
Reputation: 72731
The general idea of replacing them with NA is correct.
You can use recode
if you want to do it in a single line:
library(car)
var <- recode( var, "c('missing','unknown','uncoded')=NA" )
Upvotes: 4