When I tried to answer this question, I came across some very strange behavior. Below I define the same data twice, once just as a data.frame
and the second time using mutate
. I check that the results are identical. Then I try to do the same filtering operation. For the first data set this works, but for the second (identical) data set it fails. Can anybody figure out why.
It seems that part of the reason for this difference is the use of ?
. But I don't understand why that is a problem for the second data set, but not for the first.
# define the same data twice
datos1 <- data.frame(a?o = 2001:2005, gedad = c(letters[1:5]), a?o2 = 2001:2005)
datos2 <- data.frame(a?o = 2001:2005, gedad = c(letters[1:5])) %>% mutate(a?o2 = a?o)
# check that they are identical
identical(datos1, datos2)
# do same operation
datos1 %>% filter(a?o2 >= 2003)
## a?o gedad a?o2
## 1 2003 c 2003
## 2 2004 d 2004
## 3 2005 e 2005
datos2 %>% filter(a?o2 >= 2003)
## Error in filter_impl(.data, dots) : object 'a?o2' not found
Note: I don't believe that this is a duplicate of the original question because I ask why this difference occurs and the original post asked how to fix it.
EDIT: Since @Khashaa could not reproduce the error, here is my sessionInfo()
output:
sessionInfo()
## R version 3.1.2 (2014-10-31)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
##
## locale:
## [1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252
## [4] LC_NUMERIC=C LC_TIME=German_Switzerland.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] dplyr_0.4.1
##
## loaded via a namespace (and not attached):
## [1] assertthat_0.1 DBI_0.3.1 lazyeval_0.1.10 magrittr_1.5 parallel_3.1.2 Rcpp_0.11.4 tools_3.1.2
See Question&Answers more detail:os