AcidCatfish
AcidCatfish

Reputation: 324

How to change from factored data to numeric?

I have this dataframe in factored form:

Data <- structure(list(ID = c("1", "2", "3", "4", "5", 
"6"), V1 = structure(c(1L, 1L, 4L, 4L, 4L, 1L), .Label = c("1", 
"129", "2", "3", "76"), class = "factor"), V2 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("1", "3"), class = "factor"), 
    V3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "1", class = "factor"), 
    V4 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
    "3"), class = "factor"), V5 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "1", class = "factor"), V6 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "1", class = "factor"), V7 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "1", class = "factor"), V8 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("0", "1", "3"), class = "factor"), 
    V9 = structure(c(2L, 2L, 3L, 2L, 2L, 2L
    ), .Label = c("0", "1", "3"), class = "factor"), V10 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("0", "1", "2", "3"), class = "factor"), 
    V11 = structure(c(2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("0", "1"), class = "factor"), V12 = structure(c(1L, 
    1L, 1L, 1L, 1L, 3L), .Label = c("1", "2", "3"), class = "factor"), 
    V13 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
    "2", "3"), class = "factor"), V14 = structure(c(1L, 
    1L, 2L, 1L, 1L, 1L), .Label = c("1", "3"), class = "factor"), 
    V15 = structure(c(2L, 2L, 2L, 2L, 2L, 
    2L), .Label = c("0", "1", "3"), class = "factor"), V17 = structure(c(3L, 
    1L, 3L, 1L, 1L, 3L), .Label = c("1", "2", "3"), class = "factor"), 
    V18 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
    "2", "3"), class = "factor"), V19 = structure(c(1L, 
    1L, 2L, 1L, 1L, 1L), .Label = c("1", "3"), class = "factor"), 
    V20 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
    "3"), class = "factor"), V21 = structure(c(1L, 3L, 
    1L, 1L, 3L, 1L), .Label = c("1", "2", "3"), class = "factor"), 
    V22 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
    "2", "3"), class = "factor"), V23 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("1", "3"), class = "factor"), 
    V24 = structure(c(1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = "1", class = "factor"), V25 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"), 
    V26 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
    "2"), class = "factor"), V27 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "1", class = "factor"), V28 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"), 
    V29 = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0", 
    "1"), class = "factor"), V30 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"), 
    V31 = structure(c(2L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("0", "1"), class = "factor"), V32 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"), 
    Totals = structure(c(1L, 1L, 4L, 1L, 2L, 2L), .Label = c("1", 
    "2", "3", "5"), class = "factor")), row.names = c(NA, 6L), class = "data.frame")

It is in factored form but I need to change it to numeric form without changing the original dataframe called Data. So, I tried this method:

Data2 <- lapply(Data[c(1:33)], numeric)

This gave me the "invalid length argument" error. So I tried this method after looking up the issue:

Data2 <- lapply(Data[c(1:33)], as.numeric)
Data2 <- as.data.frame(Data2)

I do indeed get a new dataframe, but the data doesn't match what I have in my script. Some numbers change by 1 value, for example. (Where there is a 3, it is a 4. Where there is a 4, there is a 5).

Any other methods to this issue?

EDIT: earlier in my script I convert from character to factor using this method:

Data <- lapply(Data[c(2:33)], factor)

Would it be easier to instead convert to numeric and wait until I am done with all of my analyses to convert to factor?

Upvotes: 0

Views: 49

Answers (3)

akrun
akrun

Reputation: 886938

We can use type.convert from base R

Data <- type.convert(Data, as.is = TRUE)

Upvotes: 1

Duck
Duck

Reputation: 39585

Try this dplyr approach:

library(dplyr)
#Code
Data2 <- Data %>% mutate(across(2:33,~as.numeric(as.character(.))))

Upvotes: 1

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

You need to convert to character first:

Data <- lapply(Data, function(x) as.numeric(as.character(x)))

Upvotes: 1

Related Questions