juba/questionr — pull request #4 by briatte (2013-07-23 17:10:23+00:00)
Title: Better recode.na function

The `recode.na` function is now more cautious with numeric strings.

Here's the new output for each example provided in the documentation.

> require(questionr)
> source("/Users/fr/Documents/Code/R/questionr/R/recode.r")
> data(hdv2003)

With exact string matches:

> freq(recode.na(hdv2003$nivetud, "Inconnu"))
Recoded 0 values to NA.
n %
N'a jamais fait d'etudes 39 2.0
A arrete ses etudes, avant la derniere annee d'etudes primaires 86 4.3
Derniere annee d'etudes primaires 341 17.1
1er cycle 204 10.2
2eme cycle 183 9.2
Enseignement technique ou professionnel court 463 23.2
Enseignement technique ou professionnel long 131 6.6
Enseignement superieur y compris technique superieur 441 22.1
NA 112 5.6

With regular expressions:

> freq(recode.na(hdv2003$relig, "[A|a]ppartenance", "Rejet|NSP"))
Recoded 1292 values to NA.
n %
Pratiquant regulier 266 13.3
Pratiquant occasionnel 442 22.1
NA 1292 64.6

Showing missing values:

> freq(recode.na(hdv2003$clso, "Ne sait pas", verbose = TRUE))
Recoded 27 values to NA.
n
Ne sait pas 27
n %
Oui 936 46.8
Non 1037 51.8
NA 27 1.4

Test results with `freq` (as shown above too):

> freq(recode.na(hdv2003$trav.satisf, "Equilibre"))
Recoded 451 values to NA.
n %
Satisfaction 480 24.0
Insatisfaction 117 5.9
NA 1403 70.2

Truncate a count variable (recommends numeric conversion):

> freq(recode.na(hdv2003$freres.soeurs, 5:22))
Recoded 505 values to NA.
Recoded variable contains only numeric characters. Consider using as.numeric = TRUE.
n %
0 167 8.3
1 407 20.3
2 427 21.3
3 284 14.2
4 210 10.5
NA 505 25.2

I'm also coding a `freq.na` function to inspect the proportion of missing values in a vector or any number of variables.