【发布时间】:2015-03-12 14:08:40
【问题描述】:
当我试图回答this question 时,我遇到了一些非常奇怪的行为。下面我定义了两次相同的数据,一次是data.frame,第二次使用mutate。我检查结果是否相同。然后我尝试做同样的过滤操作。对于第一个数据集,这是可行的,但对于第二个(相同的)数据集,它会失败。任何人都可以弄清楚为什么。
似乎造成这种差异的部分原因是使用了ñ。但我不明白为什么这对第二个数据集来说是个问题,而不是对第一个数据集。
# 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
注意:我不认为这是原始问题的重复,因为我问为什么会出现这种差异,而原始帖子询问如何解决它。
编辑:由于@Khashaa 无法重现错误,这是我的sessionInfo() 输出:
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
【问题讨论】:
-
无法重现错误。
-
就是这样。它使用机器的系统语言环境,将
ñ翻译成其他字符。在这种情况下,字符可能看起来相同,但我敢打赌它具有不同的 ASCII 值。在我的希腊机器中,我在错误消息中得到ρ而不是ñ。我可以使用datos2 %>% filter("aρo2" >= 2003)进行查询 -
我怀疑机器的代码页是 850 或类似的东西,它使用与 Windows 1252 或 437 不同的变音符号位置
-
我明白为什么它们看起来一样,但内部表示不同。但是为什么
identical没有发现差异呢?那么为什么datos2 %>% subset(año2>=2003)会起作用呢? -
现在就做——尽管在 R 决定像其他人一样使用 Unicode 之前不要使用非英文字符