I wanted to modify levels in my factor variable by grouping two levels into one when I came across this strange situation. Basically, my new level is created, but all the remaining levels seem to be moved to the next one. Here is my example data, the code used and the output.
library(tidyverse)
data <- structure(list(factor1 = structure(c(1L, 1L, 2L, 3L, 1L, 2L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 1L, 1L, 1L, 4L), .Label = c("0", "1", "2", "3",
"4", "5", "6", "7"), class = "factor")), row.names = c(NA, -30L
), class = c("tbl_df", "tbl", "data.frame"), .Names = "factor1")
data_out <- data %>% mutate(factor1 = ifelse(factor1 %in% c('0', '1'),
factor1, '>1'))
structure(list(factor1 = c("1", "1", "2", ">1", "1", "2", "1",
"1", "2", "2", "2", "2", "2", "1", "2", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", ">1", "1", "1", "1", ">1")), .Names = "factor1",
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -30L))
Is it desirable behaviour? It certainly isn't in my case. How could it be explained and then corrected?