0

I read a .csv file where there is a Has_Cancer variable encoded as "No" and "Yes".

Using the command:

read.csv("data_04_Cancer.csv", sep = ',', header = TRUE) 

the variable 'Has_Cancer' comes with the class 'chr'.

Then I transform that as a factor using:

data_to_work$Has_Cancer <- factor(data_to_work$Has_Cancer, label = c("No", "Yes"),levels = c(0,1))

So the result is ok: Has_Cancer: Factor w/2 levels "No", "Yes": NA NA NA...

I don't understand why 'NA' appears, I think the correct one would be 0,0,0,1,1,1... instead of NA.

would someone clarify please

Phil
  • 7,287
  • 3
  • 36
  • 66
Elias
  • 11
  • 2
  • 3
    Check this post: [Confusion between factor levels and factor labels](https://stackoverflow.com/questions/5869539/confusion-between-factor-levels-and-factor-labels) – Quinten Jul 05 '22 at 12:26
  • Thank you, I discovered my error: the data on .csv file has "No", "Yes" instead of 0 and 1. So for experience I changed it for 0 and 1, and applied the same command ahd is appears 1 and 2 instead 0 and 1. Confused again ! – Elias Jul 05 '22 at 12:58

0 Answers0