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.

3

3 Answers 3

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]))
     }
Sign up to request clarification or add additional context in comments.

1 Comment

You can't directly nest them, but you can define an anonymous function like sapply(df, function(x) as.numeric(as.character(x)) )
2

This should also work:

df[] <- lapply(df, function(x) ifelse(is.numeric(x), as.numeric(x), x))

Comments

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.