I want to write a function that is doing the same as the SPSS command AUTORECODE.
AUTORECODE recodes the values of string and numeric variables to consecutive integers and puts the recoded values into a new variable called a target variable.
At first I tried this way:
AUTORECODE <- function(variable = NULL){
A <- sort(unique(variable))
B <- seq(1:length(unique(variable)))
REC <- Recode(var = variable, recodes = "A = B")
return(REC)
}
But this causes an error. I think the problem is caused by the committal of A and B to the recodes argument. Thats why I tried
eval(parse(text = paste("REC <- Recode(var = variable, recodes = 'c(",A,") = c(",B,")')")))
within the function. But this isn´t the right solution.
Ideas?
factormay be simply what you need, as James suggested in a comment, it’s storing them as integers behind the scenes (as seen bystr) and just outputting the corresponding labels. This may also be very useful as R has lots of commands for working with factors appropriately, such as when fitting linear models, it makes all the “dummy” variables for you.If you do just need the numbers, use
as.integeron the factor.An alternate solution is to use
match, but if you’re starting with floating-point numbers, watch out for floating-point traps.factorconverts everything to characters first, which effectively rounds floating-point numbers to a certain number of digits, making floating-point traps less of a concern.