使用sapply的不可重现的错误

时间:2016-03-16 20:45:13

标签: r

我遇到的情况是我将数据从字符转换为整数。然而,在一台机器上,相同的代码产生的结果与另一台机器上的结果不同,而我却为此而感到茫然。这是一个例子:

df <- data.frame(
  a = "1", b = "2", c = "3", 
  stringsAsFactors = FALSE
)

现在,在我的本地计算机上,以下工作正常:

df[,c("a","b")] <- sapply(df[,c("a","b")], as.integer)

str(df)
'data.frame':   1 obs. of  3 variables:
 $ a: int 1
 $ b: int 2
 $ c: chr "3"

但是在远程计算机上,同一行代码返回:

df[,c("a","b")] <- sapply(df[,c("a","b")], as.integer)
str(df)
'data.frame':   1 obs. of  3 variables:
 $ a: chr "1"
 $ b: chr "2"
 $ c: chr "3"

我发现这很奇怪,我无法想出任何好的理由。我查看了sys.info(),两者之间的所有包都是一样的。与区域设置相同。唯一的区别是R的版本和Ubuntu的版本

我的机器:R 3.2.2,Ubuntu 15.10

R version 3.2.2 (2015-08-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 15.10

locale:
 [1] LC_CTYPE=C                 LC_NUMERIC=C               LC_TIME=C
 [4] LC_COLLATE=C               LC_MONETARY=C              LC_MESSAGES=en_CA.UTF-8
 [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                  LC_ADDRESS=C
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages: 
[1] dplyr_0.4.3          tidyr_0.4.1          data.table_1.9.6    
[4] RMySQL_0.9-3         DBI_0.3.1  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3       codetools_0.2-14  psych_1.5.8       foreach_1.4.3
 [5] assertthat_0.1    plyr_1.8.3        chron_2.3-47      R6_2.1.2
 [9] magrittr_1.5      lazyeval_0.1.10   doParallel_1.0.10 iterators_1.0.8
[13] tools_3.2.2       yaml_2.1.13       parallel_3.2.2    mnormt_1.5-3

远程:R 3.2.4,Ubuntu 12.04

R version 3.2.4 (2016-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C         LC_TIME=C            LC_COLLATE=C        
 [5] LC_MONETARY=C        LC_MESSAGES=C        LC_PAPER=C           LC_NAME=C           
 [9] LC_ADDRESS=C         LC_TELEPHONE=C       LC_MEASUREMENT=C     LC_IDENTIFICATION=C 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.3          tidyr_0.4.1          data.table_1.9.6    
[4] RMySQL_0.9-3         DBI_0.3.1           

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3      codetools_0.2-14 psych_1.4.8.11   foreach_1.4.2    assertthat_0.1  
 [6] plyr_1.8.1       chron_2.3-47     R6_2.0.1         magrittr_1.5     lazyeval_0.1.10 
[11] doParallel_1.0.8 iterators_1.0.7  tools_3.2.4      yaml_2.1.13      parallel_3.2.4 

思想?

0 个答案:

没有答案
相关问题