Reputation: 1
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
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