[R] strange behavior of print vs. round

William Revelle lists at revelle.net
Fri Feb 6 17:46:32 CET 2009


Dear friends,
   I have discovered what I find peculiar behavior of print.  In 
general, print with n digits agrees with round with n digits.

Although printing all the elements of a vector with digits=2 gives 
the expected results, printing a single element of the vector does 
not.

>  x <- seq(.194,.994,.1)
>  x
[1] 0.194 0.294 0.394 0.494 0.594 0.694 0.794 0.894 0.994

>  print(x,2)     #this makes sense
[1] 0.19 0.29 0.39 0.49 0.59 0.69 0.79 0.89 0.99 
>  for(i in 1:9) print(x[i],2)   #but this does not
[1] 0.19
[1] 0.29
[1] 0.39
[1] 0.49
[1] 0.59
[1] 0.7
[1] 0.8
[1] 0.9
[1] 1

#     But this is not a problem with round .
>  for(i in 1:9) print(round(x[i],2))
[1] 0.19
[1] 0.29
[1] 0.39
[1] 0.49
[1] 0.59
[1] 0.69
[1] 0.79
[1] 0.89
[1] 0.99



>  sessionInfo()
R version 2.8.1 Patched (2008-12-31 r47414)
i386-apple-darwin8.11.1

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] psych_1.0-64

loaded via a namespace (and not attached):
[1] tools_2.8.1

Bill


-- 
William Revelle		http://personality-project.org/revelle.html
Professor			http://personality-project.org/personality.html
Department of Psychology             http://www.wcas.northwestern.edu/psych/
Northwestern University	http://www.northwestern.edu/
Use R for psychology                       http://personality-project.org/r




More information about the R-help mailing list