Welcome to R programming! R is a powerful language for statistical computing and data analysis.
Vectors in R
Vectors are the most basic data structure in R. They contain elements of the same type.
Exercise 1: Creating Vectors
# Create numeric and character vectors
ages <- c(25, 30, 35, 40)
names <- c("Alice", "Bob", "Charlie", "Diana")
print(ages)
print(names)
print(mean(ages))
Basic Operations
R makes it easy to perform operations on entire vectors at once.
Exercise 2: Vector Math
# Perform calculations on vectors
prices <- c(10, 20, 30, 40)
tax_rate <- 0.08
total_prices <- prices * (1 + tax_rate)
print(total_prices)
print(sum(total_prices))
Data Frames
Data frames are like tables - they store data in rows and columns.
Exercise 3: Creating a Data Frame
# Create a simple data frame
students <- data.frame(
name = c("John", "Mary", "Steve"),
age = c(20, 22, 21),
grade = c(85, 92, 78)
)
print(students)
print(students$name)
Fantastic! You’ve learned the fundamentals of R programming.