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.
## [1] 4
## [1] 5
## [1] 12
## [1] 5
## [1] 8
# Print results with descriptive labels using print() function
print("Addition: 2 + 2 =") # Print a text label## [1] "Addition: 2 + 2 ="
## [1] 4
## [1] "Exponentiation: 2^3 ="
## [1] 8
# 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:"
## [1] 10
## [1] "Value of my_number:"
## [1] 42
## [1] "Value of text:"
## [1] "Hello, 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':"
## [1] "numeric"
## [1] "Type of 'name':"
## [1] "character"
## [1] "Type of 'is_student':"
## [1] "logical"
# 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:"
## [1] 1 2 3 4 5
## [1] "Numbers + 2:"
## [1] 3 4 5 6 7
## [1] "Numbers * 3:"
## [1] 3 6 9 12 15
## [1] "First number:"
## [1] 1
## [1] "Second fruit:"
## [1] "banana"
# 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:"
## [1] 1 2 3 4 5
# 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:"
## numbers letters
## 1 1 A
## 2 2 B
## 3 3 C
## 4 4 D
## 5 5 E
# 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:"
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] "Mean of my vector:"
## [1] 5.5
# 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:"
## $numbers
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $colors
## [1] "red" "blue" "green"
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)