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!
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"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.