Statistics with R

R is both a programming language and software environment for statistical computing, which is free and open-source. To get started, you will need to install two pieces of software:

  • R, the actual programming language. – Chose your operating system, and select the most recent version. Read how to install R in Windows system from here.
  • RStudio, an excellent IDE for working with R. – Note, you must have R installed to use RStudio. RStudio is simply an interface used to interact with R. Learn how to install RStudio from here.

Once your R environment is ready to install a package, use the install.packages() function.

Suppose “ggplot2” is a package used for visualization. And you want to install “ggplot2” in your R environment then write install.packages(“ggplot2”).

#Install ggplot2 package  
install.packages("ggplot2")

Once a package is installed, it must be loaded into your current R session before being used.

 

#Load the ggplot2 package
library(ggplot2)

To get started, we’ll use R like a simple mathematical tool. We will show how to do basic calculation in R.

Basic Calculations

In R, you can perform basic calculations using arithmetic operators. Here are the basic arithmetic operators in R:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: ^
  • Modulus (remainder after division): %%
  • Integer division (quotient after division): %/%

How to do Addition in R?

You can do addition in R using the + operator. Here’s an example:

 

# Addition of two numbers
2 + 3
# Output: 5

# Addition of two variables
x <- 4
y <- 5
x + y
# Output: 9

In the first example, we’re directly adding two numbers using the + operator. In the second example, we’re first assigning the values 4 and 5 to variables x and y, respectively. Then we’re using the + operator to add the values of x and y. The output is 9, which is the sum of 4 and 5.

You can also use the sum() function in R to add multiple numbers or a vector of numbers. Here’s an example:

 

# Addition using the sum() function
sum(1, 2, 3, 4, 5)
# Output: 15

# Addition using a vector
vec <- c(1, 2, 3, 4, 5)
sum(vec)
# Output: 15

How to do Subtraction in R?

You can perform subtraction in R using the - operator. Here’s an example:

 

# Subtraction of two numbers
5 - 3
# Output: 2

# Subtraction of two variables
x <- 7
y <- 5
x - y
# Output: 2

In the first example, we’re directly subtracting 3 from 5 using the - operator. In the second example, we’re first assigning the values 7 and 5 to variables x and y, respectively. Then we’re using the - operator to subtract the value of y from x. The output is 2, which is the result of 7 minus 5.

You can also perform subtraction using the diff() function in R, which calculates the difference between consecutive elements in a vector. Here’s an example:

# Subtraction using the diff() function
vec <- c(2, 4, 6, 8, 10)
diff(vec)
# Output: 2 2 2 2

How to do Multiplication in R?

You can perform multiplication in R using the * operator. Here’s an example:

 

# Multiplication of two numbers
2 * 3
# Output: 6

# Multiplication of two variables
x <- 7
y <- 5
x * y
# Output: 35

In the first example, we’re directly multiplying 2 and 3 using the * operator. In the second example, we’re first assigning the values 7 and 5 to variables x and y, respectively. Then we’re using the * operator to multiply the value of x and y. The output is 35, which is the result of 7 multiplied by 5.

You can also perform multiplication using the prod() function in R, which calculates the product of the elements in a vector. Here’s an example:

# Multiplication using the prod() function
vec <- c(1, 2, 3, 4, 5)
prod(vec)
# Output: 120

In this example, we’re first creating a vector vec with the values 1, 2, 3, 4, and 5 using the c() function. Then we’re using the prod() function to calculate the product of the elements in the vector. The output is 120, which is the product of 1, 2, 3, 4, and 5.

How to do Division in R?

 

You can perform division in R using the / operator. Here’s an example:

# Division of two numbers
10 / 2
# Output: 5

# Division of two variables
x <- 7
y <- 5
x / y
# Output: 1.4

In the first example, we’re directly dividing 10 by 2 using the / operator. In the second example, we’re first assigning the values 7 and 5 to variables x and y, respectively. Then we’re using the / operator to divide the value of x by y. The output is 1.4, which is the result of 7 divided by 5.

Note that the division operator / always returns a floating-point number as the result. If you want to perform integer division, you can use the %/% operator. Here’s an example:

# Integer division using the %/% operator
7 %/% 5
# Output: 1

In this example, we’re using the %/% operator to perform integer division of 7 by 5. The output is 1, which is the integer quotient of 7 divided by 5. If you want to get the remainder of the integer division, you can use the modulo operator %. Here’s an example:

# Modulo operation using the %% operator
7 %% 5
# Output: 2

In this example, we’re using the %% operator to get the remainder of 7 divided by 5. The output is 2, which is the remainder.

Exponents in R

You can perform exponentiation in R using the ^ operator or the exp() function. Here’s an example:

 

# Exponentiation using the ^ operator
2^3
# Output: 8

# Exponentiation using the exp() function
exp(2)
# Output: 7.389056

# Exponentiation of a variable
x <- 2
y <- 3
x^y
# Output: 8

In the first example, we’re using the ^ operator to calculate 2 raised to the power of 3, which is 8. In the second example, we’re using the exp() function to calculate the exponential function of 2, which is approximately 7.389056.

In the third example, we’re first assigning the values 2 and 3 to variables x and y, respectively. Then we’re using the ^ operator to calculate the value of x raised to the power of y. The output is 8, which is the result of 2 raised to the power of 3.

Note that the ^ operator and the exp() function both perform exponentiation, but they work differently. The ^ operator calculates the value of the base raised to the power of the exponent, whereas the exp() function calculates the exponential function of the argument.

Mathematical Constants in R

R provides several built-in mathematical constants that you can use in your calculations. Here are some examples:

 

# Value of pi
pi
# Output: 3.141593

# Value of Euler's number (e)
exp(1)
# Output: 2.718282

# Value of the golden ratio (phi)
phi <- (1 + sqrt(5))/2
phi
# Output: 1.618034

In the first example, we’re using the built-in constant pi to get the value of pi, which is approximately 3.141593. In the second example, we’re using the exp() function to get the value of Euler’s number (e), which is approximately 2.718282. In the third example, we’re calculating the value of the golden ratio (phi) by adding 1 to the square root of 5, and then dividing the result by 2. The value of phi is approximately 1.618034.

You can also define your own constants in R using the = or <- operator. Here’s an example:

# Define a new constant
G <- 6.67430e-11 # Gravitational constant in SI units (m^3 kg^-1 s^-2)

# Use the new constant in a calculation
m1 <- 5.97e24 # Mass of the Earth in kg
m2 <- 70 # Mass of a person in kg
r <- 6.371e6 # Radius of the Earth in meters
F <- G * m1 * m2 / r^2 # Gravitational force between the Earth and a person
F
# Output: 686.2589

In this example, we’re defining a new constant G using the <- operator. The value of G is the gravitational constant in SI units (m^3 kg^-1 s^-2). Then we’re using the new constant G in a calculation to calculate the gravitational force between the Earth and a person. The output is 686.2589, which is the gravitational force in Newtons.

Logarithms in R

 

 

The basic logarithmic function in R is log(), which computes the natural logarithm (base e) of a given number. The general syntax for using the log() function is:

log(x, base)

where x is the input value and base is the optional base of the logarithm. If base is not specified, it defaults to base = e.

Here are some examples:

# Compute the natural logarithm of 2
log(2)

# Compute the logarithm base 10 of 100
log(100, base = 10)

# Compute the logarithm base 2 of 8
log(8, base = 2)

R also has functions for computing other types of logarithms. For example, log10() computes the base-10 logarithm, and log2() computes the base-2 logarithm.

Here are some examples:

# Compute the base-10 logarithm of 100
log10(100)

# Compute the base-2 logarithm of 8
log2(8)

Note that the log() function returns a complex number if the input is negative or zero, whereas log10() and log2() return NaN (not a number) for negative or zero inputs.

Trigonometry in R

R is a programming language that provides a variety of functions for performing trigonometric calculations. The most commonly used trigonometric functions in R are:

  • sin(x): returns the sine of x, where x is in radians
  • cos(x): returns the cosine of x, where x is in radians
  • tan(x): returns the tangent of x, where x is in radians
  • asin(x): returns the inverse sine of x, where x is in radians
  • acos(x): returns the inverse cosine of x, where x is in radians
  • atan(x): returns the inverse tangent of x, where x is in radians
  • atan2(y, x): returns the angle between the positive x-axis and the point (x,y) in radians

Note that R uses radians to represent angles in trigonometric functions. To convert from degrees to radians, you can use the function pi/180 which gives the conversion factor.

 

Here’s an example of using some of these functions in R:

# calculate the sine and cosine of pi/4
sin(pi/4)
cos(pi/4)

# calculate the tangent of pi/3
tan(pi/3)

# calculate the inverse sine of 0.5
asin(0.5)

# calculate the angle between (1,1) and the positive x-axis
atan2(1,1)

Getting Help in R

In using R as a calculator, we have seen a number of functions: sqrt(), exp(), log() and sin(). To get documentation about a function in R, simply put a question mark in front of the function name and RStudio will display the documentation, for example:

?log
?sin
?cos

R Objects, Numbers, Attributes, Vectors, Coercion