0

Its simple question but looks like it doesn't exist in the stackoverflow

I have a data frame where all columns are factors I want to convert it to decimals.

  Var1 Var2 Var3 Var4
1 0.76 0.84 0.76 0.73
2 0.76 0.84 0.76 0.73
3 0.76 0.84 0.76 0.73
4 0.76 0.84 0.76 0.73
5 0.76 0.84 0.76 0.73
6 0.76 0.84 0.76 0.73

I want to convert this without loosing the decimals.

df <- sapply(df, as.numeric)

This doesn't retain the decimals.

add-semi-colons
  • 18,094
  • 55
  • 145
  • 232

3 Answers3

2

If they are truly factors, you need to go through another step:

The reason for as.numeric not working directly is because internally each factor is stored by its levels . you can access that through the levels(factor_var). So when you apply as.numeric to a factor directly, what gets returned is their levels. Therefore, first make it a character, and then apply as.numeric

df <- sapply(df, as.character)
df <- sapply(df, as.numeric)

Or you can nest them in a function:

convert_func<-function(x){  as.numeric(as.character(x))}

then :df <- sapply(df, convert_func)

I have never tried nesting them in the apply/lapply/sapply without a function, but it might work also. or you can make a loop:

for (col in 1:ncol(df){ 
     df[col]<-as.numeric(as.character(df[col]))
     }
joel.wilson
  • 8,243
  • 5
  • 28
  • 48
sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • You can't directly nest them, but you can define an anonymous function like `sapply(df, function(x) as.numeric(as.character(x)) )` – thelatemail Jan 25 '17 at 04:57
2

This should also work:

df[] <- lapply(df, function(x) ifelse(is.numeric(x), as.numeric(x), x))
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
1

We can use dplyr to convert the factor columns to numeric

library(dplyr)
library(magrittr)
df %<>%
   mutate_if(is.factor, funs(as.numeric(as.character(.))))

With base R, we can do

df[] <- lapply(df, function(x) if(is.factor(x)) as.numeric(as.character(x)) else x)

data

df <- structure(list(Var1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), 
.Label = "0.76", class = "factor"), 
Var2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "0.84", class = "factor"), 
Var3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "0.76", class = "factor"), 
Var4 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "0.73", class = "factor")),
 .Names = c("Var1", "Var2", "Var3", "Var4"), row.names = c("1", "2", "3", "4", "5", 
"6"), class = "data.frame")
akrun
  • 874,273
  • 37
  • 540
  • 662