ah bon
ah bon

Reputation: 10061

Convert multiple character columns into numeric in R

Given a dataframe as follows:

ks3score ks4score ethnic gender SECshort
28.54   214.00  Indian  Male    Intermediate    
39.84   543.00  Indian  Male    Intermediate    
28.34   294.00  Mixed heritage  Male    Intermediate    
35.78   348.00  Mixed heritage  Male    Intermediate    
35.79   388.00  Mixed heritage  Male    Intermediate    
24.67   504.00  Pakistani   Male    Intermediate    
20.39   70.25   White British   Male    Intermediate    
27.25   92.00   White British   Male    Intermediate    
19.61   104.00  White British   Male    Intermediate    
19.61   150.00  White British   Male    Intermediate

I want to convert character variables into numeric variables, I have used with the code below but it convert them to NA.

cols <- c("ethnic", "gender", "SECshort")
df[cols] <- lapply(df[cols], factor)
df[, cols]<-lapply(cols, function(x) as.numeric(as.character(df[,x])))

Out:

ks3score ks4score ethnic gender SECshort
28.54   214.00  NA  NA  NA
39.84   543.00  NA  NA  NA
28.34   294.00  NA  NA  NA
35.78   348.00  NA  NA  NA
35.79   388.00  NA  NA  NA
24.67   504.00  NA  NA  NA
20.39   70.25   NA  NA  NA
27.25   92.00   NA  NA  NA
19.61   104.00  NA  NA  NA
19.61   150.00  NA  NA  NA

Just wonders why I get this error? Thanks.

Upvotes: 0

Views: 691

Answers (3)

akrun
akrun

Reputation: 887951

We can use mutate_at from dplyr

library(dplyr)
df <- df %>%
         mutate_at(vars(cols), as.integer)

Or with across

df <- df %>%
        mutate(across(cols, as.integer))

Upvotes: 1

linog
linog

Reputation: 6226

You have the possibility to use data.table:

library(data.table)
setDT(df)
df[,cols := lapply(.SD, as.numeric), .SDcols = cols]

You update by reference your subset of data (meaning of .SD) by applying as.numeric function

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 389325

Probably, you were trying to do :

df[cols] <- lapply(df[cols], function(x) as.integer(factor(x)))

You can also use :

df[cols] <- lapply(df[cols], function(x) match(x, unique(x)))

Upvotes: 1

Related Questions