I have a dataset which has several variables for years of birth (recorded as factors). I want to convert the factor variables to numeric, but keeping the levels. I can do this individually using the command:
data$yrbrn1 <- as.numeric(levels(data$yrbrn1))[data$yrbrn1]
However, I want to do the same thing for multiple variables.
The head of my data is (first eight variables):
   yrbrn1  yrbrn2  yrbrn3  yrbrn4  yrbrn5  yrbrn6  yrbrn7  yrbrn8
1   2012    1949    1955     NA      NA      NA      NA       NA
2   2012    1983    1951    1956    1989    1995    2003     2005
3   2012    1946    1946    1978     NA      NA      NA       NA 
4   2012     NA      NA      NA      NA      NA      NA       NA
5   2012    1953    1959    1980    1985    1991    2008     2011
6   2012    1938     NA      NA      NA      NA      NA       NA
I have tried:
data[,2:ncol(data)] <- lapply(data[,2:ncol(data)], as.numeric(levels([,2:ncol(data)]))[,2:ncol(data)]
but I get an error.
 
    