Fundamentals of R

Basic Syntax

R is a leading language for statistical computing and data analysis, supported by over 10,000 free packages in the CRAN repository. Like other programming languages, R has a specific syntax that is crucial to understand to leverage its powerful features.

Before proceeding, ensure that R is installed on your system. We’ll use RStudio as our environment, though you can also use the R command prompt by executing the following command in your terminal:

$ R
Basic Hello World Program

Type the following in your R console:

cat("Hello, World!\n")

Output:

Hello, World!

You can achieve the same result using print() as follows:

print("Hello, World!")

Typically, we write our code in scripts, saved as .R files. To execute a script, save the following code as helloWorld.R and run it in the console with:

Rscript helloWorld.R

Code:

print("Hello, World!")

Output:

[1] "Hello, World!"
Syntax of R Programs

An R program consists of three primary components:

  1. Variables
  2. Comments
  3. Keywords
Variables in R

Previously, we directly printed output using print(). However, to reuse or manipulate data, we use variables. Variables act as named memory locations to store data. In R, variables can be assigned using three operators:

  • = (Simple Assignment)
  • <- (Leftward Assignment)
  • -> (Rightward Assignment)

Example:

# Simple Assignment
a = 10
print(a)

# Leftward Assignment
b <- 20
print(b)

# Rightward Assignment
30 -> c
print(c)

Output:

[1] 10
[1] 20
[1] 30

While the rightward assignment operator is available, it’s less common and might confuse some developers. Therefore, it’s advisable to use = or <- for assigning values.

Comments in R

Comments are crucial for enhancing code readability and are ignored by the R interpreter.

  • Single-line Comments: Start with #.
  • Multi-line Comments: Though R does not natively support multi-line comments, you can achieve this effect using a trick, such as enclosing text within triple quotes inside if(FALSE) blocks.

Example:

# This is a single-line comment
print("This is an example!")

if (FALSE) {
  "This is a multi-line comment."
  "It will not be executed."
}

Output:

[1] "This is an example!"
Keywords in R

Keywords are reserved words in R that hold a specific meaning and cannot be used as variable or function names.

To view all reserved keywords, use one of the following commands in your R console:

help("reserved")

Or:

?reserved

Commonly Used Keywords:

  • Control-flow and Function Declarationifelserepeatwhilefunctionforinnext, and break.
  • Boolean ConstantsTRUE and FALSE.
  • Special ValuesNaN (Not a Number), NULL (Undefined Value), and Inf (Infinity).

Example:

# Using control-flow
if (TRUE) {
  print("Condition is TRUE!")
}

# Using a special value
x <- Inf
print(x)

Output:

[1] "Condition is TRUE!"
[1] Inf

R Comments

Comments are plain English sentences that are added within a program to explain what the code does or what a specific piece of code is designed to achieve. These are intended for the programmer’s reference and have no impact on the actual logic or execution of the code.

All programming languages use specific symbols to denote comments. When the compiler encounters these symbols, it identifies the text as a comment and ignores it during execution.

Comments in R

In the R programming language, comments are general statements written to describe the purpose or functionality of the code. They provide information for coders while having no effect on the logic or execution of the code, as comments are entirely ignored by the compiler.

How Does the Compiler Recognize a Comment?

The compiler identifies a comment based on the symbol used. In R, the # symbol is used to denote a comment.

Uses of Comments

Comments are generally used for the following purposes:

  1. Improving Code Readability
  2. Providing Explanations or Metadata
  3. Preventing Execution of Code
  4. Including Resources or Notes
Types of Comments

In programming, comments are typically categorized as:

  1. Single-line Comments: A comment that occupies only one line.
  2. Multi-line Comments: A comment that spans multiple lines.
  3. Documentation Comments: Comments that provide a detailed explanation or serve as a quick reference for the code’s functionality.
Single-Line Comments in R

Single-line comments are used to explain a single line of code. In R, any statement starting with # is treated as a comment.

Syntax:

# comment statement

Example 1:

# This is a comment

The above line will not produce any output because the compiler will treat it as a comment and ignore it.

Example 2:

# R program to calculate the sum of two numbers

# Assigning values to variables
a <- 15
b <- 5

# Printing the sum
print(a + b)

Output:

[1] 20
Multi-line Comments in R

As mentioned earlier, R does not have a built-in syntax for multi-line comments. However, multiple single-line comments can be used to achieve the same effect. R provides tools to make this process easier, especially in RStudio.

Method 1: Using Keyboard Shortcut

To comment on multiple lines at once, select the lines you wish to comment and press Ctrl + Shift + C (Windows/Linux) or Cmd + Shift + C (Mac). This will add or remove # at the beginning of the selected lines.

Example:

# This is a multi-line comment
# Each line starts with the '#' symbol
# The following code calculates and prints the sum

x <- 25
y <- 15
sum <- x + y
print(sum) # This line prints the value of 'sum'

Output:

[1] 40

Method 2: Using RStudio GUI

  1. Highlight the lines you want to comment.
  2. Go to the Code menu in the toolbar.
  3. Select Comment/Uncomment Lines from the dropdown.

R Operators

Operators in R are symbols that instruct the compiler to execute specific operations between operands. They simulate mathematical, logical, and decision-based operations on integers, complex numbers, and vectors.

Types of Operators in R
  1. Arithmetic Operators
  2. Logical Operators
  3. Relational Operators
  4. Assignment Operators
  5. Miscellaneous Operators
1. Arithmetic Operators:

Arithmetic operators are used for basic mathematical calculations and operate element-wise when used with vectors.

Addition (+): Adds corresponding elements of two operands.

x <- c(1.2, 0.5)
y <- c(3.4, 1.5)
print(x + y)

Output:

4.6  2.0

Subtraction (-): Subtracts elements of the second operand from the first.

x <- 10.5
y <- 4.3
print(x - y)

Output:

6.2

Multiplication (*): Multiplies corresponding elements.

a <- c(2, 3)
b <- c(4, 5)
print(a * b)

Output:

8  15

Division (/): Divides the first operand by the second.

a <- 9
b <- 3
print(a / b)

Output:

3

Power (^): Raises the first operand to the power of the second.

c

Output:

9

Modulo (%%): Returns the remainder of division of the first operand by the second.

x <- c(7, 12)
y <- c(3, 5)
print(x %% y)

Output:

1  2

Example Code:

x <- c(3, 7)
y <- c(2, 5)
cat("Addition:", x + y, "\n")
cat("Subtraction:", x - y, "\n")
cat("Multiplication:", x * y, "\n")
cat("Division:", x / y, "\n")
cat("Modulo:", x %% y, "\n")
cat("Power:", x ^ y)

Output:

Addition: 5 12
Subtraction: 1 2
Multiplication: 6 35
Division: 1.5 1.4
Modulo: 1 2
Power: 9 16807
2. Logical Operators in R

Logical operators in R are used for performing element-wise logical operations between values. These operators evaluate conditions and return a boolean result, either TRUE or FALSE. Any nonzero numeric value (real or complex) is treated as TRUE, whereas zero is considered FALSE.

Element-wise Logical AND (&): This operator returns TRUE only if both elements being compared are TRUE.

Example:

val1 <- c(FALSE, 5)
val2 <- c(3+2i, 0)
print(val1 & val2)

Output:

FALSE  FALSE

Element-wise Logical OR (|): This operator returns TRUE if at least one of the two elements is TRUE.

Example:

val1 <- c(FALSE, 5)
val2 <- c(3+2i, 0)
print(val1 | val2)
TRUE  TRUE

Logical NOT (!): A unary operator that reverses the logical value of each element.

Example:

val1 <- c(1, FALSE)
print(!val1)
FALSE  TRUE
3. Relational Operators

Relational operators compare elements of two operands and return boolean values.

Less Than (<): Returns TRUE if elements of the first operand are less than the second.

x <- c(2, 5)
y <- c(3, 5)
print(x < y)

Output:

TRUE FALSE

Greater Than (>): Returns TRUE if elements of the first operand are greater than the second.

x <- c(6, 4)
y <- c(3, 4)
print(x > y)

Output:

TRUE FALSE

Equal To (==): Checks if elements are equal.

x <- c(2, 4)
y <- c(2, 5)
print(x == y)

Output:

TRUE FALSE

Example Code:

x <- c(4, 7)
y <- c(5, 7)
cat("Less than:", x < y, "\n")
cat("Greater than:", x > y, "\n")
cat("Equal to:", x == y, "\n")

Output:

Less than: TRUE FALSE
Greater than: FALSE FALSE
Equal to: FALSE TRUE
4. Assignment Operators

Left Assignment (<-=): Assigns values to a variable.

x <- c(1, 2, 3)
y = c(4, 5, 6)
print(x)
print(y)

Output:

[1] 1 2 3
[1] 4 5 6

Right Assignment (->->>): Assigns values from left to right.

c(7, 8, 9) -> z
print(z)

Output:

[1] 7 8 9
5. Miscellaneous Operators

%in%: Checks if an element exists in a list or vector.

x <- 3
y <- c(1, 2, 3, 4)
print(x %in% y)

Output:

TRUE

%*%:Matrix multiplication operator.

mat <- matrix(1:6, nrow = 2)
print(mat)
print(t(mat))
result <- mat %*% t(mat)
print(result)

Output:

[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
     [,1] [,2]
[1,]   35   44
[2,]   44   56

R Keywords

R is an open-source programming language extensively utilized for statistical computing and data analysis. It features a command-line interface and is supported on platforms such as Windows, Linux, and macOS. As a cutting-edge tool, R is highly regarded for its versatility.

Keywords in R

Keywords are reserved words in R with predefined functionalities. These words are critical for leveraging the capabilities of the R programming language. You can view the complete list of keywords in R using either help(reserved) or ?reserved. Below is a comprehensive list of keywords in R:

if, else, while, repeat, for, function, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_complex_, NA_character_, ...
Key R Keywords with Examples

1. if: The if statement evaluates a condition and executes a block of code if the condition is true.
Example:

# Check if a number is positive
number <- 10
if (number > 0) {
    cat("The number is positive\n")
}

Output:

The number is positive
The number is positive
The number is positive

2. else: The else statement executes a block of code if the if condition evaluates to false.
Example:

# Check if a number is positive or non-positive
num <- -2
if (num > 0) {
    cat(num, "is positive\n")
} else {
    cat(num, "is not positive\n")
}

Output:

-2 is not positive

3. while: The while loop runs repeatedly until the condition becomes false.
Example:

# Print numbers from 1 to 4
count <- 1
while (count <= 4) {
    cat(count, "\n")
    count <- count + 1
}

Output:

1
2
3
4

4. repeat: The repeat loop runs indefinitely until a break statement is encountered.
Example:

# Print numbers from 1 to 3 using repeat
num <- 1
repeat {
    cat(num, "\n")
    if (num == 3) {
        break
    }
    num <- num + 1
}

Output:

1
2
3

5. for: The for loop iterates over a sequence.
Example:

# Display square of numbers from 1 to 3
for (val in 1:3) {
    cat("Square of", val, "is", val^2, "\n")
}

Output:

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9

6. function: Functions allow you to encapsulate reusable code blocks.
Example:

# Function to calculate factorial
factorial <- function(n) {
    if (n == 0) return(1)
    return(n * factorial(n - 1))
}
cat("Factorial of 5 is", factorial(5), "\n")

Output:

Factorial of 5 is 120

7. next: The next statement skips the current iteration and proceeds to the next.
Example:

# Skip even numbers from 1 to 5
for (i in 1:5) {
    if (i %% 2 == 0) next
    cat(i, "\n")
}

Output:

1
3
5

8. break: The break statement terminates a loop.
Example: 

# Terminate loop when i equals 3
i <- 1
while (TRUE) {
    cat(i, "\n")
    if (i == 3) break
    i <- i + 1
}

Output:

1
2
3

9. TRUE/FALSE: These keywords represent Boolean values.
Example:

# Logical comparison
x <- 7
y <- 5
cat(x > y, "\n")  # TRUE
cat(x < y, "\n")  # FALSE

Output:

TRUE
FALSE

10. NULL: The NULL keyword represents a null or undefined object.
Example:

# Example of NULL
data <- NULL
cat("Value of data is", data, "\n")

Output:

Value of data is NULL

11. Inf and NaN: Inf and NaN represent infinity and “not a number,” respectively.
Example:

# Check for infinity and NaN
nums <- c(1, Inf, NaN, 4)
cat(is.finite(nums), "\n")  # Check finite
cat(is.nan(nums), "\n")     # Check NaN

Output:

TRUE FALSE FALSE TRUE
FALSE FALSE TRUE FALSE

12. NA: NA represents missing values.
Example:

# Identify missing values
vec <- c(10, NA, 20, 30)
cat(is.na(vec), "\n")

Output:

FALSE TRUE FALSE FALSE

R Data Types

In programming, data types define and categorize the various forms of data that can be saved and manipulated. R programming provides a range of data types, each with unique properties and operations.

What are R Data Types?

R data types specify the kind of data a variable can store. Selecting the appropriate data type ensures efficient memory use and accurate computations. Variables in R do not need an explicitly declared data type and can have their types dynamically changed.

Data Types in R Programming Language

Every variable in R has a data type. These types dictate the memory requirements and the operations allowed.

Common R Data Types:

  1. Numeric – Represents decimal values (e.g., 3.14121.5).
  2. Integer – Denotes whole numbers (e.g., 2L42L, where L declares integers).
  3. Logical – Boolean values (TRUEFALSE).
  4. Complex – Numbers with imaginary components (e.g., 7 + 5i).
  5. Character – Strings or text values (e.g., "Hello""123").
  6. Raw – Binary data represented at the byte level (e.g., as.raw(255)).

1. Numeric Data Type: Decimal numbers are considered numeric in R. It is the default data type for numbers.

# Example of Numeric Data Type
x <- 7.8

# Class and type
print(class(x))     # Output: "numeric"
print(typeof(x))    # Output: "double"

Even assigning an integer to a variable will treat it as numeric:

y <- 10
print(class(y))      # Output: "numeric"
print(typeof(y))     # Output: "double"
print(is.integer(y)) # Output: FALSE

2. Integer Data Type: Integer values are explicitly represented using the L suffix or as.integer() function.

# Example of Integer Data Type
a <- as.integer(25)
b <- 50L

# Class and type
print(class(a))  # Output: "integer"
print(typeof(a)) # Output: "integer"
print(class(b))  # Output: "integer"

3. Logical Data Type: Logical values represent boolean outcomes, such as TRUE or FALSE.

# Example of Logical Data Type
x <- 12
y <- 8
result <- x > y

# Logical output
print(result)     # Output: TRUE
print(class(result)) # Output: "logical"

4. Complex Data Type: Complex numbers have both real and imaginary parts.

# Example of Complex Data Type
c <- 3 + 4i

# Class and type
print(class(c))  # Output: "complex"
print(typeof(c)) # Output: "complex"

5. Character Data Type: Character strings can include text, symbols, or numbers treated as text.

# Example of Character Data Type
text <- "Data Science"

# Class and type
print(class(text))  # Output: "character"
print(typeof(text)) # Output: "character"

6. Raw Data Type: Raw data types are used for low-level binary operations.

# Example of Raw Data Type
raw_vec <- as.raw(c(0x10, 0x20, 0x30))
print(raw_vec)  # Output: 10 20 30
Checking Data Type in R

Use the class() function to find an object’s data type:

# Data type examples
print(class(TRUE))         # Output: "logical"
print(class(50L))          # Output: "integer"
print(class(15.75))        # Output: "numeric"
print(class(2+3i))         # Output: "complex"
print(class("R Language")) # Output: "character"
Coercing or Converting Data Types

R allows converting one data type to another using as.<data_type>(). Note: Not all conversions are valid, and invalid ones return NA.

# Data type conversion
print(as.numeric(TRUE))     # Output: 1
print(as.complex(10L))      # Output: 10+0i
print(as.logical(0))        # Output: FALSE
print(as.character(4+3i))   # Output: "4+3i"
print(as.numeric("Text"))   # Output: NA

Warning:

Warning message:
NAs introduced by coercion

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *