Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rstudio and R terminal give different outputs

Tags:

r

unique

rstudio

In Rstudio (using R 3.1.1) when I run this,

length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9220 

In R 3.1.1 when I run this,

length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9183

(the correct output is 9183)

I can't figure out why... help is greatly appreciated

like image 653
nathanesau Avatar asked Dec 02 '14 20:12

nathanesau


People also ask

What is the difference between console and terminal in RStudio?

In simple words: In console, you can run your R commands. In terminal, you can access your system shell.

How do I export console output in R?

You can also save the entire R console screen within the GUI by clicking on "Save to File..." under the menu "File." This saves the commands and the output to a text file, exactly as you see them on the screen.

How do I change the terminal in RStudio?

RStudio supports multiple terminal sessions. To start another terminal session, use the New Terminal command on the Terminal dropdown menu, or Alt+Shift+R.

What is R console and RStudio?

RStudio is an integrated development environment (IDE) for R. It includes a console, syntax-highlighting editor that supports direct code execution, as well as tools for plotting, history, debugging and workspace management.


1 Answers

As David Arenburg notes, this is a difference between 32-bit and 64-bit R versions, at least on Windows machines. Presumably, some sort of rounding error is involved. Interestingly, it is the 32-bit R gets the answer right, whereas the 64-bit R finds too many unique numbers.

First to confirm that 9183 is indeed the correct answer, I used the gmp package (a wrapper for the C multiple precision arithmetic library GMP), which provides results that are not subject to rounding errors:

library(gmp)
x <- as.bigz(2:100)
length(unique(do.call(c, sapply(x, function(X) x^X))))
[1] 9183

Here are the results from my 32-bit R:

length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9183
R.version[1:7]               _                           
# platform       i386-w64-mingw32            
# arch           i386                        
# os             mingw32                     
# system         i386, mingw32               
# status                                     
# major          3                           
# minor          1.2                         

And here are the results from my 64-bit R:

length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9220
R.version[1:7]
# platform       x86_64-w64-mingw32          
# arch           x86_64                      
# os             mingw32                     
# system         x86_64, mingw32             
# status                                     
# major          3                           
# minor          1.2                         
like image 77
Josh O'Brien Avatar answered Nov 12 '22 16:11

Josh O'Brien