Hi there,
I am trying to mutate one variable in a dataframe into a categorical value without changing the other variables.
Here is some sample data:
DF <-data.frame(
ID = c(1L, 2L, 3L, 4L, 5L, 6L),
Software = c(1L, 0L, 0L, 0L, 1L, 0L),
Expertise = c(1L, 3L, 1L, NA, 2L, 4L),
Confidence = c(2L, NA, 4L, 5L, 3L, 2L)
)
I normally use this code
DF %>% dplyr::mutate_if(is.numeric, factor, levels = 1:5, labels = lbs) %>% drop_na() %>% as.data.frame()
but this only works if the other columns are not numeric (including the ID variable). In the above dataframe, both Expertise and Confidence are categorical variables using different scales - hence the need to only mutate one column at a time. I've tried this:
DF %>%
dplyr::mutate_at(vars(contains("Expertise")), ~(1=="Not a barrier", 2=="Minor barrier", 3=="Major barrier", 4=="No answer"))%>%
as.data.frame()
and
DF %>%
dplyr::mutate_at(vars(contains("Expertise")), funs(1=="Not a barrier", 2=="Minor barrier", 3=="Major barrier", 4=="No answer"))%>%
as.data.frame()
which produces:
Any help or advice would be much appreciated.