When I create a factor X using summarise() and then use that factor to derive another variable Z, I realize that summarise() will not recognize the levels of factor X.
For example
# create fake dataset tmp <- data.frame(id = 1:3, x1 = c(1, 0, 0), x2 = factor(c("Yes", "Yes", "No"), levels = c("Yes", "No"))) # test summarise() tmp2 <- tmp %>% group_by(id) %>% summarise(y1 = ifelse(x1 > 0, "Yes", "No"), y2 = factor(ifelse(x1 > 0, "Yes", "No"), levels = c("Yes", "No")), y3 = ifelse(x2 == "Yes" & y1 == "Yes", "Yes", "No"), y4 = ifelse(x2 == "Yes" & y2 == "Yes", "Yes", "No"), y5 = ifelse(x2 == "Yes" & y2 == 1, "Yes", "No") ) tmp2
give
# A tibble: 3 × 6
id y1 y2 y3 y4 y5
<int> <chr> <fctr> <chr> <chr> <chr>
1 1 Yes Yes Yes No Yes
2 2 No No No No No
3 3 No No No No No
y4 is clearly wrong, and somehow summarise() only recognizes that y2 has 2 values: 1, 2 and does not recognize its levels
My sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8
[8] LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_0.5.0