Using R as a Calculator

In the Windows Operating system, The R installer will have created an icon for R on the desktop and a Start Menu item. Double-click the R icon to start the R Program; R will open the console, to type R commands.

The greater than sing (>) in the console is the prompt symbol. In this tutorial, we will use the R language as a calculator (we will be Using R as a Calculator for mathematical expressions), by typing some simple mathematical expressions at the prompt (>). Anything that can be computed on a pocket calculator can also be computed at the R prompt. After entering the expression on the prompt, you have to press the Enter key from the keyboard to execute the command.

Using R Language As a Calculator

Some examples using R as a calculator are as follows

> 1 + 2   #add two or more numbers
> 1 - 2   #Substracts two or more numbers
> 1 * 2   #multiply two or more numbers
> 1 / 2   #divides two more more numbers
> 1 %/% 2 #gives the integer part of the quotient
> 2 ^ 1   #gives exponentiation
> 31 %% 7 #gives the remainder after division

These operators also work fine for complex numbers.

Upon pressing the enter key, the result of the expression will appear, prefixed by a number in a square bracket:

> 1 + 2
[1] 54

The [1] indicates that this is the first result from the command.

One can also use R as an advanced scientific calculator. Some advanced calculations that are available in scientific calculators can also be easily done in R for example,

> sqrt(5)      #Square Root of a number
> log(10)      #Natural log of a number
> sin(45)      #Trignometric function (sin function)
> pi           #pi value 3.141593
> exp(2)       #Antilog, e raised to a power
> log10(5)     #Log of a number base 10
> factorial(5) #Factorial of a number e.g 5!
> abs(1/-2)    #Absolute values of a number 
> 2*pi/360     #Number of radian in one Babylonian degree of a circle

Remember R prints all very large or very small numbers in scientific notation.

Order of Precedence/ Operations

The R language also makes use of parentheses for grouping operations to follow the rules for the order of operations. for example

> 1 - 2/3   #It first computes 2/3 and then subtracts it from 1
> (1-2)/3   #It first computes (1-2) and then divides it by 3

The R Language recognizes certain goofs, like trying to divide by zero, missing values in data, etc.

> 1/0   #Undefined, R tells it an infinity (Inf)
> 0/0   #Not a number (NaN) 
> "one"/2   #Strings or characters is divided by a number

Further Reading: Computing Descriptive Statistics in R

Online MCQs Computer Science with Answers

Introduction to R Language

Introduction to R Language

What is R (Language)

R is an open-source (GPL) programming language for statistical computing and graphics, made after S and S-plus language. The S language was developed by AT&T laboratories in the late ’80s. Robert Gentleman and Ross Ihaka started the research project of the statistics department of the University of Auckland in 1995 called R Language.

The R language is currently maintained by the R core development team (an international team of volunteer developers). The (R Project website) is the main site for information about R. From this page information about obtaining the software, accompanying package, and many other sources of documentation (help files) can be obtained.

R provides a wide variety of statistical and graphical techniques such as linear and non-linear modeling, classical statistical tests, time-series analysis, classification, multivariate analysis, etc., as it is an integrated suite of software having facilities for data manipulation, calculation, and graphics display. It includes

  • Effective data handling and storage facilities
  • Have a suite of operators for calculation on arrays, particularly for matrices
  • Have a large, coherent, integrated collection of intermediate tools for data analysis
  • Graphical data analysis
  • Conditions, loops, user-defined recursive functions, and input-output facilities.

Obtaining R Software

R program can be obtained/downloaded from the R Project site the ready-to-run (binaries) files for several operating systems such as Windows, Mac OS X, Linux, Solaris, etc. The source code for R is also available for download and can be compiled for other platforms. R language simplifies many statistical computations as R is a very powerful statistical language having many statistical routines (programming code) developed by people from all over the world and are freely available from the R project website as “Packages”. The basic installation of R language contains many powerful sets of tools and it includes some basic packages required for data handling and data analysis.

Many users of R think of R as a statistical system, but it is an environment within which statistical techniques are implemented. The R language can also be extended via packages.

Installing R

For Windows, the operating system binary version is available from http://cran.r- project.org/bin/windows/base/. “R-3.0.0-win.exe. R-3.0.0” is the latest version of R released on 03 April 2013 by Duncan Murdoch.
After downloading the binary file double-click it, and almost automatic installation of the R system will start although the customized installation option is also available. Follow the instructions during the installation procedure. Once the installation process is complete, you have the R icon on your computer desktop.

The R Console

When R starts, you will see R console windows, where you type commands to get the required results. Note that commands are typed on the R Console command prompt. You can also edit the commands previously typed on the command prompt by using the left, right, up, and down arrow keys, home, end, backspace, insert, and delete keys from the keyboard. Command history can be obtained by up and down arrow keys to scroll through recent commands. It is also possible to type commands in a file and then execute the file, using the source function in the R console.

Books on R Programming Language

The following books can be useful for learning the R and S language.

  • “Psychologie statistique avec R” by Yvonnick Noel. Partique R. Springer, 2013.
  • “Instant R: An introduction to R for Statistical Analysis” by Sarah Stowell. Jotunheim Publishing, 2012.
  • “Financial Risk Modeling and Portfolio Optimization with R” by Bernhard Pfaff. Wiley, Chichester, UK, 2012.
  • “An R Companion to Applied Regression” by John Fox and Sanford Weisberg, Sage Publications, Thousand Oaks, CA, USA, 2nd Edition, 2011,
  • “R Graphs Cookbook” by Hrishi Mittal, Packt Publishing, 2011
  • “R in Action” by Rob Kabacoff. Manning, 2010.
  • “The Statistical Analysis with R Beginners Guide” by John M. Quick. Packt Publishing, 2010.
  • “Introducing Monte Carlo Methods with R” by Christian Robert and George Casella. Use R. Springer, 2010.
  • “R for SAS and SPSS users” by Robert A. Muenchen. Springer Series in Statistics and Computing. Springer, 2009.

Reading Creating and Import Data in R

There are many ways to read data into R-Language.  We will learn here how to import data in R Language too. We can also generate certain kinds of patterned data. Some of them are

Reading Data from the Keyboard Directly

For small data (few observations) one can input data in vector form directly on R Console, such as

x <- c(1,2,3,4,5)
y <-c('a', 'b', 'c')

In vector form, data can be on several lines by omitting the right parentheses, until the data are complete, such as

x <-c(1,2 
      3,4)

Note that it is more convenient to use the scan function, which permits the index of the next entry.

Using Scan Function

For small data sets it is better to read data from the console by using the scan function. The data can be entered on a separate line, by using a single space and/or tab. After entering the complete required data, pressing twice the enter key will terminate the scanning.

X <-scan()
     3 4 5
     4 5 6 7
     2 3 4 5 6 6

Reading String Data using the “what” Option

y <- scan(what=" ")
      red green blue
      white

The scan function can be used to import data. The scan function returns a list or a vector while read.table function returns a data frame. It means that the scan function is less useful for imputing “rectangular” type data.

Reading data from ASCII or plain text files into R as Data Frame

The read.table function reads any type of delimited ASCII file. It can be numeric and character values. Reading data into R by read.table is the easiest and most reliable method. The default delimiter is a blank space.

data <-read.table(file=file.choose()) #select from dialog box
data <-read.table("http://itfeature.com/test.txt", header=TRUE)) # read from web site

Note that read.table command can also be used for reading data from the computer disk by providing an appropriate path in inverted commas such as

data <-read.table("D:/data.txt", header=TRUE)) # read from your computer

For missing data, read.table will not work and you will receive an error. For missing values the easiest way to fix this error, change the type of delimiter by using a sep argument to specify the delimiter.

data <-read.table("http//itfeature.com/missing_comma.txt", header=TRUE, sep=","))

Comma-delimited files can be read in by read.table function and sep argument, but it can also be read in by the read.csv function specifically written for comma-delimited files. To display the contents of the file use print() function or file name.

data <-read.csv(file=file.choose())

Reading in fixed formatted files

To read data in fixed format use read.fwf function and argument width are used to indicate the width (number of columns) for each variable. In this format variable names are not there in the first line, therefore they must be added after reading the data. Variable names are added by dimnames function and the bracket notation to indicate that we are attaching names to the variables (columns) of the data file. Anyhow there are several different ways to do this task.

data <-read.fwf("http://itfeature.com/test_fixed.txt", width=c(8,1,3,1,1,1))
dimnames(data)[[2]]
c("v1", "v2", "v3", "v4", "v5","v6")

Import Data In R

Importing data in R is fairly simple. For Stata and Systat, use the foreign package. For SPSS and SAS recommended package is the Hmisc package for ease and functionality. See the Quick-R section on packages, for information on obtaining and installing these packages. Examples of importing data in R are provided below.

From Excel

On Windows systems, you can use the RODBC package to access Excel files. The first row of the Excel file should contain variable/column names.

# Excel file name is myexcel and WorkSheet name is mysheet
library(RODBC)
channel <- odbcConnectExcel("c:/myexel.xls")
mydata <- sqlFetch(channel, "mysheet") 
odbcClose(channel)
From SPSS
# First save SPSS dataset in trasport format
get file='c:\data.sav'
export outfile='c:\data.por' 
library(Hmisc)
mydata <- spss.get("c:/data.por", use.value.labels=TRUE)   # "use.value.labels" option converts value labels to R factors.
From SAS
# save SAS dataset in trasport format
libname out xport 'c:/mydata.xpt';
data out.data;
set sasuser.data;
run;

# in R
library(Hmisc)
mydata <- sasxport.get("c:/data.xpt")
# character variables are converted to R factors

From Stata
# input Stata file
library(foreign)
mydata <- read.dta("c:/data.dta")

From systat
# input Systat file
library(foreign)
mydata <- read.systat("c:/mydata.dta")
Importing Data in R

Accessing Data in R Library

Many of the R libraries including CAR library contain data sets. For example to access the Duncan data frame from the CAR library in R type the following command on R Console

library(car)
data(Duncan)
attach(Duncan)

Some Important Commands for Dataframes

data        #displays the entire data set on command editor
head(data)  #displays the first 6 rows of dataframe
tail(data)  #displays the last 6 rows of dataframe
str(data)   #displays the names of variable and their types
names(data) #shows the variable names only
rename(V1,Variable1, dataFrame=data) # renames V1 to variable 1; note that epicalc packagemust be installed
ls()        #shows a list of objects that are available
attach(data)#attached the dataframe to the R search path, which makes it easy to access variables names.

MCQs R Language

Online Multiple Choice Questions about R Programming Language

1. When using RStudio, what does the installed.packages()function do?

 
 
 
 

2. A data analyst wants to write R code that they can access again after they close their current session in RStudio. Where should they write their code?

 
 
 
 

3. A data analyst is searching for a tool that will allow them to communicate instructions that a computer can run. What tool should they use?

 
 
 
 

4. Which of the following are the benefits of open-source code? Select all that apply.

 
 
 
 

5. What tool gives data analysts the highest level of control over their data analysis?

 
 
 
 

6. A data analyst needs to quickly create a series of scatterplots to visualize a very large dataset. What should they use for the analysis?

 
 
 
 

7. Why do analysts use comments In R programming? Select all that apply.

 
 
 
 

8. What are the benefits of using a programming language to work with your data? Select all that apply.

 
 
 
 

9. A data analyst wants to use a programming language that they can modify. What type of programming language should they use?

 
 
 
 

10. Programming involves _____ a computer to perform an action or set of actions.

 
 
 
 

11. If you write code directly in the R source editor, RStudio can save your code when you close your current session.

 
 

12. A data analyst is searching for an open-source tool that will allow them to reproduce every step of their analysis, including data cleaning and transformations, calculations, and visualizations. What tool is the best option?

 
 
 
 

13. A data analyst is working with spreadsheet data. The analyst imports the data from the spreadsheet into RStudio. Where in RStudio can the analyst find the imported data?

 
 
 
 

14. Many data analysts prefer to use a programming language for which of the following reasons? Select all that apply.

 
 
 
 

15. What type of software application is RStudio?

 
 
 
 

16. RStudio includes which of the following panes? Select all that apply.

 
 
 
 

17. In data analytics, what is CRAN?

 
 
 
 

18. The R programming language can be used for which of the following tasks? Select all that apply.

 
 
 
 

19. What are the benefits of using a programming language for data analysis? Select all that apply.

 
 
 
 

20. An analyst includes the following calculation in their R programming: midyear_sales <- (quarter_1_sales + quarter_2_sales) - overhead_costsWhich variable will the total from this calculation be assigned to?

 
 
 
 

Question 1 of 20

Scroll to top
x  Powerful Protection for WordPress, from Shield Security
This Site Is Protected By
Shield Security