Alice
Alice

Reputation: 57

convert several factors where the levels appear as numbers to a numeric vector

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.

Upvotes: 1

Views: 35

Answers (1)

G5W
G5W

Reputation: 37641

First of all, I don't think that you want to use as.numeric. Look at the results of that on any one column and you will see what is wrong. Instead you need to use as.character first.

Also, data is the name of an R function, so I am going to assume that the data is called DATA instead. You should get what you want from

 sapply(DATA, function(x) as.numeric(as.character(x)))

Upvotes: 1

Related Questions