rlevels

R Replacing certain factor levels in data.table with NA


I need to replace factors with the value 0 and 128 in a certain column (LU1945) in a very large data table (val) but as you can see this approach does not work as the levels are still there when I call levels. I tried NA and 'NA' but still the levels are there.

levels(val$LU1945)[val$LU1945== 0] <- NA
levels(val$LU1945)[val$LU1945== 128] <- NA

levels(val$LU1945)[val$LU1945== 0] <- 'NA'
levels(val$LU1945)[val$LU1945== 128] <- 'NA'

#check to see if levels were replaced
levels(val$LU1945)
[1] "0"   "1"   "2"   "3"   "6"   "7"   "8"   "9"   "10"  "11"  "13"  "14" 
[13] "15"  "128"

Solution

    1. 'NA' is a string literal, not the same as NA. Don't use quotes.
    2. Replace the values, not the levels. (NA is native in factors anyway, there's no real need to inject it literally into the levels.)
    vec <- factor(1:10)
    vec
    #  [1] 1  2  3  4  5  6  7  8  9  10
    # Levels: 1 2 3 4 5 6 7 8 9 10
    vec[vec %in% c(4, 8)] <- NA
    vec
    #  [1] 1    2    3    <NA> 5    6    7    <NA> 9    10  
    # Levels: 1 2 3 4 5 6 7 8 9 10
    

    You can relevel if you'd like,

    vec <- factor(vec)
    vec
    #  [1] 1    2    3    <NA> 5    6    7    <NA> 9    10  
    # Levels: 1 2 3 5 6 7 9 10