R programming - Basics

R Syntax

R has a syntax that allows users to perform calculations and create variables to store data. Here is how you can assign values to variables and print them:

# Assigning values to variables
x <- 10
y <- 5
# Printing variables
print(x)
[1] 10
print(y)
[1] 5
# Commenting
# This is a single line comment

Data Types

R supports various data types, including numeric, character, logical, and more. Here’s how you can create different types of variables:

# Numeric
a <- 42

# Character
b <- "Hello, R!"

# Logical
c <- TRUE

# Printing types
print(typeof(a))
[1] "double"
print(typeof(b))
[1] "character"
print(typeof(c))
[1] "logical"

Data Structures

# Vector
my_vector <- c(1, 2, 3, 4, 5)
print(my_vector)
[1] 1 2 3 4 5
# List
my_list <- list(title="Basics of R", views=1000, tags=c("R", "programming"))
print(my_list)
$title
[1] "Basics of R"

$views
[1] 1000

$tags
[1] "R"           "programming"
# Matrix
my_matrix <- matrix(1:9, nrow=3, ncol=3)
print(my_matrix)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
# Data Frame
my_data <- data.frame(names=c("Alice", "Bob", "Charlie"), ages=c(25, 32, 37))
print(my_data)
    names ages
1   Alice   25
2     Bob   32
3 Charlie   37

Basic Operations

# Arithmetic operations
x <- 10
y <- 5
sum <- x + y
difference <- x - y
product <- x * y
quotient <- x / y
remainder <- x %% y
power <- x^2

print(sum)
print(difference)
print(product)
print(quotient)
print(remainder)
print(power)

Functions

# Defining a function
add_numbers <- function(num1, num2) {
  return(num1 + num2)
}

# Calling a function
result <- add_numbers(20, 30)
print(result)
[1] 50

Conditional Stataments

# If statement example
x <- 20
if (x > 15) {
  print("x is greater than 15")
}
[1] "x is greater than 15"
# If-else statement example
y <- 10
if (y > 15) {
  print("y is greater than 15")
} else {
  print("y is not greater than 15")
}
[1] "y is not greater than 15"
# If- else if-else statement example
z <- 10
if (z > 15) {
  print("z is greater than 15")
} else if (z < 5) {
  print("z is less than 5")
} else {
  print("z is between 5 and 15")
}
[1] "z is between 5 and 15"
# Nested if example
a <- 20
if (a > 10) {
  if (a < 30) {
    print("a is greater than 10 and less than 30")
  }
}
[1] "a is greater than 10 and less than 30"

Loops

# For loop example
for (i in 1:5) {
  print(paste("Value of i is:", i))
}
# While loop example
count <- 1
while (count <= 5) {
  print(paste("Count is:", count))
  count <- count + 1  # Increment the count
}
# Repeat loop example
num <- 1
repeat {
  print(paste("Number is:", num))
  num <- num + 1
  if (num > 5) {
    break  # Exit the loop
  }
  #Break Statement
}
for (i in 1:10) {
  if (i == 6) {
    break  # Exit the loop if i equals 6
  }
  print(i)
}
#Next Statement
for (i in 1:10) {
  if (i %% 2 == 0) {  # Skip even numbers
    next
  }
  print(i)
}

Collections in R

In R, collections refer to data types that can hold multiple values. The most common collections in R are vectors, lists, matrices, data frames, and factors.

Vectors

Vectors are the simplest type of collection in R and can contain elements of the same type (numeric, character, logical, etc.). They are one-dimensional arrays that can be created using the c() function.

# Creating a numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)

# Creating a character vector
character_vector <- c("apple", "banana", "cherry")

# Creating a logical vector
logical_vector <- c(TRUE, FALSE, TRUE, FALSE)

Lists

Lists can contain elements of different types, such as numbers, strings, vectors, and even other lists. They are created using the list() function.

# Creating a list
mixed_list <- list(name="John", age=25, scores=c(90, 80, 85))

Matrices

Matrices are two-dimensional arrays that contain elements of the same type. They are created using the matrix() function, specifying the number of rows and columns.

# Creating a matrix
my_matrix <- matrix(1:9, nrow=3, ncol=3)  # Creates a 3x3 matrix

Data Frames

Data frames are similar to matrices but can contain different types of elements in each column, much like a spreadsheet. They are created using the data.frame() function.

# Creating a data frame
my_data_frame <- data.frame(
  names=c("Alice", "Bob", "Charlie"),
  ages=c(24, 30, 35),
  employed=c(TRUE, TRUE, FALSE)
)

Factors

Factors are used to handle categorical data and store both the raw data and the levels of the categories. They are created using the factor() function.

# Creating a factor
gender <- factor(c("male", "female", "female", "male"))
  • Vectors are used for simple lists of values.
  • Lists are used when you need a collection of elements that can be different types.
  • Matrices are used for mathematical computations that require a two-dimensional array structure.
  • Data frames are the most commonly used data structure for datasets in R, as they can hold different types of data which correspond to variables.
  • Factors are essential for statistical modeling and graphics when dealing with categorical data.