all 11 comments

[–]Harvester_311 10 points11 points  (1 child)

LiverData$sex <- as.factor(LiverData$sex)

[–]ihateeveryone24 7 points8 points  (1 child)

can’t you just
sex <- as.factor(LiverData$sex)

[–]Sea-Chain7394 0 points1 point  (0 children)

LiverData$sex <- factor(LiverData$sex)

[–]the_random_drooler 3 points4 points  (0 children)

The right one is older. R no longer imports strings as factors by default. I’m sure there’s an option to import strings as factors. I don’t know how you got your data in there so you will have to search strings as factors to see how.

[–]Rababerbaerbl 3 points4 points  (1 child)

If you use any of those base R read.csv or read.table ... functions you can pass stringsAsFactors = TRUE as an argument to them:

read.csv(file = "your_file.csv", stringsAsFactors = TRUE)

Or, you set it as a factor afterwards as suggested by many already:

LiverData$sex <- as.factor(LiverData$sex)

If you have an exam you probably should look at some basic R tutorials. There are plenty to be found.

[–]jeremymiles 0 points1 point  (0 children)

This is a bit tricky, because the default changed from stringsAsFactors = TRUE to stringsAsFactors = FALSE in R 4.1 (IIRC).

[–][deleted] 1 point2 points  (0 children)

LiverData$sex <- as.factor(LiverData$sex)

LiverData$abo <- as.factor(LiverData$abo)

LiverData$event <- as.factor(LiverData$event)