

I tried this: lbls <- sort(levels(df))ĭf <- as.numeric(sub("^\\(0*(+)\\).+$", "\\1", df))ĭf <- (df, lbls)īut when I do this, I get the warning "Warning message:

How can I convert all of them to numeric using this syntax? Great, that works for one single variable! But there are 2613 variables that all need to be converted to numeric. Is there a way to specify all variables instead of a single variable ("MYVAR")? I don't want to have to repeat this procedure for every variable, because there are 2613.įor example, I can successfully use this to convert the single variable "C1PAA2J" from factor to numeric: class(df$C1PAA2J)ĭf$C1PAA2J <- as.numeric(sub("^\\(0*(+)\\).+$", "\\1", df$C1PAA2J))ĭf$C1PAA2J <- (df$C1PAA2J, lbls)

I am trying to figure out a way to do this for ALL the variables in the dataframe, but I haven't been able to. (5) Add value labels, making this a named numeric vector" lbls <- sort(levels(da99999.0001$MYVAR)) (4) Strip labels from data, leaving only the original codes, e.g., "(01) STRONGLY DISAGREE" becomes "STRONGLY DISAGREE" (3) Strip original codes from lbls, leaving only the value labels, e.g., Necessary) were preserved in the factor levels. Sort will be numeric as original codes (zero-padded, if (2) Create object (lbls) containing the factor levels for the specified Substitute the actual variable and data frame names for da99999.0001$MYVAR Numeric categorical variable to factor during R data frame generation. ICSPR to the factor levels in the process of converting the original This works because the original numeric codes were prepended by
CONVERT FACTOR TO NUMERIC R CODE
They have provided code to convert the variables from factor to numeric:Ĭonvert R factor variable back to numeric in an ICPSR-produced R dataįrame. The dataset consists of around 2600 variables (demographic, health, etc.) for around 4,000 subjects. The dataset is downloaded from this link and includes the conversion code.
