Recoding NAs in a factor variable

Hello,

I am trying to replace NAs in a factor variable by 999. I have tried using both ds.recodeValues and ds.replaceNA to do this, but haven’t been successful with either. Please could somebody advise me where I’m going wrong? My code is below…

Thanks in advance!

ds.recodeValues(

  • var.name = “D3$edu_m_”,
  • values2replace.vector = c(1,NA),
  • new.values.vector = c(1,999),
  • newobj = ‘edu_m_noNA’,
  • datasources = opals
  • ) $is.object.created [1] “A data object <edu_m_noNA> has been created in all specified data sources”

$validity.check [1] “<edu_m_noNA> appears valid in all sources”

ds.levels(“edu_m_noNA”) $dnbc [1] “1” “2” “3”

$inma [1] “1” “2” “3”

$ninfea [1] “1” “2” “3”

$repro_pl [1] “1” “2” “3”

$raine [1] “1” “2” “3”

$moba [1] “1” “2” “3”

$sws [1] “1” “2” “3”

ds.numNA(“edu_m_noNA”) $dnbc [1] 16877

$inma [1] 94

$ninfea [1] 66

$repro_pl [1] 7

$raine [1] 211

$moba [1] 7648

$sws [1] 9

##Alternatively with ds.replaceNA: ds.replaceNA(x = ‘D3$edu_m_’, forNA=list(999,999,999,999,999,999,999), newobj =‘edu_m_noNA’) –Processing dnbc… –Processing inma… –Processing ninfea… –Processing repro_pl… –Processing raine… –Processing moba… –Processing sws… ds.levels(“edu_m_noNA”) $dnbc [1] “1” “2” “3”

$inma [1] “1” “2” “3”

$ninfea [1] “1” “2” “3”

$repro_pl [1] “1” “2” “3”

$raine [1] “1” “2” “3”

$moba [1] “1” “2” “3”

$sws [1] “1” “2” “3”

ds.numNA(“edu_m_noNA”) $dnbc [1] 16877

$inma [1] 94

$ninfea [1] 66

$repro_pl [1] 7

$raine [1] 211

$moba [1] 7648

$sws [1] 9

Hi Angela,

I don’t expect the functions ds.recodeLevels or ds.recodeValues to replace the missing values in your example, as the NAs are usually not considered as another category of a factor variable.

The function that should work is the ds.replaceNA for which I tried something similar with your example and it also not operating as expected for factor variables. You can follow the steps below, until I further investigate where the issue exist:

  1. Convert the categorical variable to a numeric variable using the ds.asNumeric function
  2. Replace missing values of the numeric variable with ‘999’ using the ds.replaceNA function
  3. Convert the last numeric variable to a factor using the ds.asFactor function.

The last step will give you a factor variable with levels 1, 2, 3 and 999, and without any missing values.