R Programming Basics

On This Page

R is a powerful programming language for statistical computing and graphics, widely used by data scientists, statisticians, and researchers. When combined with R Studio, an integrated development environment (IDE) designed for R, it becomes an indispensable tool for data analysis and visualization. In this article, So you need R and R Studio.

Install

  1. Download and Install R
  2. RStudio Desktop

Ok. R Studio is straightforward and intuitive

Script Editor: The top-left panel is the Script Editor. This is where you write and edit your R scripts. To create a new script, go to File > New File > R Script.

R Console: The left panel is the R Console, where you can type and execute R commands directly. You can think of it as an interactive interface where you can experiment with R code.

To display the value of a variable, you can use the print() function or simply type the variable name. Both methods will output the value of the variable.

x <- 42
# Print the value of x using the print() function
print(x)
# Print the value of x by typing the variable name
x

Variables

In R, variables are used to store data values. You can assign a value to a variable using the assignment operator <- or =. Here’s an example,

# Assigning values to variables
x <- 10
y <- 5
z = 15

Data Types

R supports various data types, including numeric, character, logical, and more. Let’s look at some examples,

# Numeric data type
num_var <- 10
# Character data type
char_var <- "Hello, world!"
# Logical data type
logical_var <- TRUE

Basic Operations

You can perform basic arithmetic operations in R, such as addition, subtraction, multiplication, and division. Here are some examples,

# Addition
sum <- x + y
# Subtraction
difference <- x - y
# Multiplication
product <- x * y
# Division
quotient <- x / y

Vectors

Vectors can hold numeric, character, or logical values. You can create vectors using the c() function.

# Numeric vector
num_vector <- c(1, 2, 3, 4, 5)
# Character vector
char_vector <- c("apple", "banana", "cherry")
# Logical vector
logical_vector <- c(TRUE, FALSE, TRUE)

Matrices

Matrices are the same type (numeric, character, or logical) in a two-dimensional layout. You can create matrices using the matrix() function.

# Creating a numeric matrix
matrix_data <- matrix(1:9, nrow = 3, ncol = 3)
print(matrix_data)
# or same from vector
# Creating a matrix from a vector
vector_data <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
matrix_data <- matrix(vector_data, nrow = 3, ncol = 3)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

Accessing Matrix Elements

You can access elements of a matrix using square brackets, specifying the row and column indices.

# Accessing a specific element (row 2, column 3)
element <- matrix_data[2, 3]
print(element)
# Accessing a specific row
row <- matrix_data[2, ]
print(row)
# Accessing a specific column
column <- matrix_data[, 3]
print(column)

Matrix Operations

You can perform various operations on matrices, including addition, subtraction, multiplication, and division. These operations are typically performed element-wise or as matrix operations (e.g., matrix multiplication).

Element-wise Operations

# Creating another matrix for operations
matrix_data2 <- matrix(9:1, nrow = 3, ncol = 3)
# Element-wise addition
matrix_sum <- matrix_data + matrix_data2
print(matrix_sum)
# Element-wise subtraction
matrix_diff <- matrix_data - matrix_data2
print(matrix_diff)
# Element-wise multiplication
matrix_prod <- matrix_data * matrix_data2
print(matrix_prod)
# Element-wise division
matrix_div <- matrix_data / matrix_data2
print(matrix_div)

Matrix Multiplication

Matrix multiplication is different from element-wise multiplication and is performed using the %*% operator.

# Matrix multiplication
matrix_mult <- matrix_data %*% matrix_data2
print(matrix_mult)

Transposing a Matrix

Transposing a matrix means swapping its rows and columns. You can transpose a matrix using the t() function.

matrix_transpose <- t(matrix_data)
print(matrix_transpose)

Data Frames

You can create a data frame using the data.frame() function. Here’s an example:

# Creating a data frame
df <- data.frame(
  Name = c("Alice", "Bob", "Charlie"),
  Age = c(25, 30, 35),
  Score = c(90, 85, 88)
)
# Viewing the data frame
print(df)

Inspecting Data Frames

# View the first few rows
head(df)
# View the last few rows
tail(df)
# View the structure of the data frame
str(df)
# Get a summary of the data frame
summary(df)

Accessing Data

You can access data in a data frame using the $ operator, square brackets [], or the subset() function.

# Accessing a column using $
print(df$Name)
# Accessing a specific element [row, column]
print(df[2, 3]) # Row 2, Column 3
# Accessing multiple rows and columns
print(df[1:2, c("Name", "Score")])

Drop Your Email

Comments

Add a Comment