Anna O'Donnell
Anna O'Donnell

Reputation: 1

Reordering levels for multiple factor variables

I'm looking to apply the following code to my data frame called factored but rather than isolating one variable I'd like to identify multiple ones:

factored$DIABETES_FAMILY <- factor(factored$DIABETES_FAMILY, levels=c("Yes","No","Missing"))

Currently the order is incorrect as "Missing" , "No", "Yes" I have many variables with this incorrect order so for example I tried executing this command for two columns as follows but it turned all my observations into NA

factored[,2:3] <- factor(factored[,2:3], levels = c("Yes", "No" , "Missing"))

Any help is much appreciated!

Upvotes: 0

Views: 48

Answers (1)

akrun
akrun

Reputation: 887981

We need to loop over the columns and assign it to the columns as factor as the x argument for factor takes a vector.

x- a vector of data, usually taking a small number of distinct values.

based on the documentation (?factor)

factored[2:3] <- lapply(factored[2:3], factor, levels = c("Yes", "No" , "Missing"))

Upvotes: 2

Related Questions