使用R的for循环中可能存在的错误

时间:2019-06-07 15:46:25

标签: r for-loop

我将R与RStudio一起使用,并在循环中得到了真正的磨损行为。我将k值从0.1循环到0.1乘10,当k等于4.4时,它会磨损。我运行以下简单代码进行检查:

#rm(list = ls())
#gc()
for(k in seq(from=0.1,to=10,by=0.1)){
  print(k)
  if(k == 4.4){
    print('a')
  }
  if(k == 4.5){
    print('a')
  }
}

输出应为从0.1到10的顺序,在4.4 a之后和4.5之后。但是仅打印对应于值4.5的值。 4.5的if语句是另一个语句的复制粘贴,仅将4改为5。我的检查代码输出为:

[1] 0.1
[1] 0.2
[1] 0.3
[1] 0.4
[1] 0.5
[1] 0.6
[1] 0.7
[1] 0.8
[1] 0.9
[1] 1
[1] 1.1
[1] 1.2
[1] 1.3
[1] 1.4
[1] 1.5
[1] 1.6
[1] 1.7
[1] 1.8
[1] 1.9
[1] 2
[1] 2.1
[1] 2.2
[1] 2.3
[1] 2.4
[1] 2.5
[1] 2.6
[1] 2.7
[1] 2.8
[1] 2.9
[1] 3
[1] 3.1
[1] 3.2
[1] 3.3
[1] 3.4
[1] 3.5
[1] 3.6
[1] 3.7
[1] 3.8
[1] 3.9
[1] 4
[1] 4.1
[1] 4.2
[1] 4.3
[1] 4.4
[1] 4.5
[1] "a"
[1] 4.6
[1] 4.7
[1] 4.8
[1] 4.9
[1] 5
[1] 5.1
[1] 5.2
[1] 5.3
[1] 5.4
[1] 5.5
[1] 5.6
[1] 5.7
[1] 5.8
[1] 5.9
[1] 6
[1] 6.1
[1] 6.2
[1] 6.3
[1] 6.4
[1] 6.5
[1] 6.6
[1] 6.7
[1] 6.8
[1] 6.9
[1] 7
[1] 7.1
[1] 7.2
[1] 7.3
[1] 7.4
[1] 7.5
[1] 7.6
[1] 7.7
[1] 7.8
[1] 7.9
[1] 8
[1] 8.1
[1] 8.2
[1] 8.3
[1] 8.4
[1] 8.5
[1] 8.6
[1] 8.7
[1] 8.8
[1] 8.9
[1] 9
[1] 9.1
[1] 9.2
[1] 9.3
[1] 9.4
[1] 9.5
[1] 9.6
[1] 9.7
[1] 9.8
[1] 9.9
[1] 10

sessionInfo()的输出是:

R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS:   /usr/lib/libblas/libblas.so.3.7.0
LAPACK: /usr/lib/lapack/liblapack.so.3.7.0

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 

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

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

other attached packages:
[1] purrr_0.3.2       sp_1.3-1          MissingData_0.1.0 zoo_1.8-6         scales_1.0.0     
[6] lubridate_1.7.4   ggplot2_3.1.1    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1            pillar_1.4.1          compiler_3.6.0        plyr_1.8.4           
 [5] iterators_1.0.10      xts_0.11-2            tools_3.6.0           gstat_2.0-2          
 [9] digest_0.6.19         tibble_2.1.2          gtable_0.3.0          lattice_0.20-38      
[13] pkgconfig_2.0.2       rlang_0.3.4           foreach_1.4.4         rstudioapi_0.10      
[17] mblm_0.12.1           withr_2.1.2           dplyr_0.8.1           stringr_1.4.0        
[21] gtools_3.8.1          grid_3.6.0            tidyselect_0.2.5      spacetime_1.2-2      
[25] glue_1.3.1            R6_2.4.0              RobustLinearReg_0.1.0 SignifReg_2.0        
[29] reshape2_1.4.3        magrittr_1.5          codetools_0.2-16      splines_3.6.0        
[33] intervals_0.15.1      MASS_7.3-51.4         assertthat_0.2.1      colorspace_1.4-1     
[37] labeling_0.3          stringi_1.4.3         lazyeval_0.2.2        munsell_0.5.0        
[41] gam_1.16              FNN_1.1.3             crayon_1.3.4 

0 个答案:

没有答案