Getting Started with R

R is a powerful programming language designed for statistical computing and data analysis. This tutorial will cover the fundamental concepts you need to get started with R programming.

Using R as a Calculator

# Basic arithmetic operations demonstration
2 + 2       # Addition operator (+)
## [1] 4
10 - 5      # Subtraction operator (-)
## [1] 5
4 * 3       # Multiplication operator (*)
## [1] 12
15 / 3      # Division operator (/)
## [1] 5
2^3         # Exponentiation operator (^) - 2 raised to power 3
## [1] 8
# Print results with descriptive labels using print() function
print("Addition: 2 + 2 =")          # Print a text label
## [1] "Addition: 2 + 2 ="
print(2 + 2)                        # Print the result
## [1] 4
print("Exponentiation: 2^3 =")      # Print another label
## [1] "Exponentiation: 2^3 ="
print(2^3)                          # Print the result
## [1] 8

Objects and Variables

# Creating objects using different assignment operators
x <- 10                # Using <- (preferred in R)
my_number = 42         # Using = (works but <- is more common in R)
text <- "Hello, R!"    # Assigning a string value

# Demonstrating how to print object values
print("Value of x:")                 # Print label
## [1] "Value of x:"
print(x)                            # Print numeric value
## [1] 10
print("Value of my_number:")         # Print label
## [1] "Value of my_number:"
print(my_number)                    # Print numeric value
## [1] 42
print("Value of text:")              # Print label
## [1] "Value of text:"
print(text)                         # Print string value
## [1] "Hello, R!"

Data Types in R

# Demonstrating different basic data types in R
age <- 25                           # Numeric type (includes integers and decimals)
name <- "John"                      # Character type (for text/strings)
is_student <- TRUE                  # Logical type (TRUE/FALSE)

# Using class() function to check data types
print("Type of 'age':")
## [1] "Type of 'age':"
print(class(age))                   # Shows "numeric"
## [1] "numeric"
print("Type of 'name':")
## [1] "Type of 'name':"
print(class(name))                  # Shows "character"
## [1] "character"
print("Type of 'is_student':")
## [1] "Type of 'is_student':"
print(class(is_student))            # Shows "logical"
## [1] "logical"

Vectors

# Creating vectors using the combine function c()
numbers <- c(1, 2, 3, 4, 5)         # Numeric vector
fruits <- c("apple", "banana", "orange")  # Character vector

# Demonstrating vector operations (vectorization)
numbers_plus_2 <- numbers + 2       # Adds 2 to each element
numbers_times_3 <- numbers * 3      # Multiplies each element by 3

# Demonstrating vector indexing (R uses 1-based indexing)
first_number <- numbers[1]          # Get first element using []
second_fruit <- fruits[2]           # Get second element using []

# Print results to show vector operations
print("Original numbers vector:")
## [1] "Original numbers vector:"
print(numbers)
## [1] 1 2 3 4 5
print("Numbers + 2:")               # Show vectorized addition
## [1] "Numbers + 2:"
print(numbers_plus_2)
## [1] 3 4 5 6 7
print("Numbers * 3:")               # Show vectorized multiplication
## [1] "Numbers * 3:"
print(numbers_times_3)
## [1]  3  6  9 12 15
print("First number:")              # Show indexing result
## [1] "First number:"
print(first_number)
## [1] 1
print("Second fruit:")              # Show indexing result
## [1] "Second fruit:"
print(second_fruit)
## [1] "banana"

Saving Objects

# Create example objects to demonstrate saving
numbers <- c(1, 2, 3, 4, 5)
fruits <- c("apple", "banana", "orange")

# Demonstrate different ways to save R objects
save(numbers, file = "my_numbers.RData")     # Save single object
save(numbers, fruits, file = "my_objects.RData")  # Save multiple objects
save.image(file = "workspace.RData")         # Save entire workspace

# Demonstrate loading saved objects
load("my_numbers.RData")                     # Load saved data
print("Loaded numbers:")
## [1] "Loaded numbers:"
print(numbers)
## [1] 1 2 3 4 5

Working with Files

# Create a sample data frame for file operations
df <- data.frame(
  numbers = 1:5,                    # Sequence 1 to 5
  letters = LETTERS[1:5]            # First 5 uppercase letters
)

# Demonstrate writing data to CSV
write.csv(df, file = "example.csv", row.names = FALSE)

# Demonstrate reading data from CSV
data <- read.csv("example.csv")     # Read the file we just created
print("Data read from CSV:")
## [1] "Data read from CSV:"
print(data)                         # Show the loaded data
##   numbers letters
## 1       1       A
## 2       2       B
## 3       3       C
## 4       4       D
## 5       5       E

Getting Help

# Demonstrate different ways to get help in R
?mean           # Access help page for mean function
help(sum)       # Alternative way to access help
example(mean)   # See examples of using mean function

Practice Exercises

Exercise 1: Create and Manipulate a Vector

# Create a sequence vector using : operator
my_vector <- 1:10                   # Creates vector from 1 to 10

# Calculate the arithmetic mean
vector_mean <- mean(my_vector)      # Use mean() function

# Display results
print("My vector:")
## [1] "My vector:"
print(my_vector)
##  [1]  1  2  3  4  5  6  7  8  9 10
print("Mean of my vector:")
## [1] "Mean of my vector:"
print(vector_mean)
## [1] 5.5

Exercise 2: Working with Different Data Types

# Create a character vector
colors <- c("red", "blue", "green")  # Vector of color names

# Create a list to hold multiple data types
my_list <- list(
  numbers = my_vector,              # Add numeric vector
  colors = colors                   # Add character vector
)

# Show the structure of the list
print("My list:")
## [1] "My list:"
print(my_list)
## $numbers
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $colors
## [1] "red"   "blue"  "green"

Next Steps

After mastering these basics, you can move on to: - Data frames and tibbles - Basic plotting with base R - Installing and using packages - Basic statistical functions - Control structures (if/else, loops)