All labs · lesson 9 of 9

Rbeginner30 min

R Basics: Vectors and Data Frames

Introduction to R programming with vectors, data frames, and basic operations.

Edit any code block and press Run (or Ctrl/⌘ + Enter, or Shift + Enter). The code runs in your browser, so there is nothing to install. The first run downloads the language once; later runs are instant.

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.

Back to all labs