John Smith
John Smith

Reputation: 2886

Converting Factors to Numbers: Warning message: NAs introduced by coercion

This question is somewhat related to a question i asked R: Using Apply Function to clean likert answers

I have a likert scale but everything is text. I wish to convert the columns to factors and then to numbers. There are missing values in the answers

df[,104:123] <- as.numeric(apply(df[,104:105], 2, 
                                  function (x) factor(x,levels =  c("NEVER","RARELY","SOMETIMES","MOST OF THE TIME","ALWAYS"))))

I however get the following error: Warning message: NAs introduced by coercion

IN fact it has converted everything to NA I ran the code without the as.numeric conversion and it seems ok

Below is the output for two columns apply(df[,104:123], 2, function(x) unique(x))

$Ans.1
[1] ""                 "SOMETIMES"        "MOST OF THE TIME"   "RARELY"           "ALWAYS"           "NEVER"           

$Ans.2.
[1] ""                 "SOMETIMES"        "MOST OF THE TIME" "RARELY"           "ALWAYS"           "NEVER" 

Can anyone see the problem in my code

Thanks

Upvotes: 1

Views: 1909

Answers (2)

akrun
akrun

Reputation: 887088

We need to use lapply instead of apply as the apply returns a matrix and matrix can have only a single class. So, instead of factor it will be all character class if there is any element that is a character. By using as.numeric on character class, we get all NAs.

df[,104:123] <- lapply(df[,104:123], function (x) 
 as.numeric(factor(x, levels =  c("NEVER","RARELY","SOMETIMES",
             "MOST OF THE TIME","ALWAYS"))))

Upvotes: 2

Daniel
Daniel

Reputation: 7832

Could you provide some reproducible data?

Else, see my example of to_value from sjmisc, which might work for you:

x <- factor(c("ALWAYS", "SOMETIMES", "NEVER","RARELY","SOMETIMES","SOMETIMES", "MOST OF THE TIME","ALWAYS", "SOMETIMES"),
            levels =  c("NEVER","RARELY","SOMETIMES","MOST OF THE TIME","ALWAYS"))

table(x)

>  NEVER     RARELY     SOMETIMES MOST OF THE TIME     ALWAYS 
>      1          1             4                1          2 

table(sjmisc::to_value(x))

> 1 2 3 4 5 
> 1 1 4 1 2 

You can also provide a data frame as argument:

df[,104:105] <- to_value(df[,104:105])

Upvotes: 0

Related Questions