R
Beginner
1 min read
Merging, Reshaping, and Applying Functions to Data Frames
Example
# cbind / rbind
left <- data.frame(id = 1:3, x = c(10, 20, 30))
right <- data.frame(id = 1:3, y = c("a", "b", "c"))
cbind(left, right[ , -1]) # add column y
extra_row <- data.frame(id = 4, x = 40)
rbind(left, extra_row) # append a row
# merge() — SQL-style joins
orders <- data.frame(
cust_id = c(1, 2, 2, 3),
amount = c(100, 200, 150, 300)
)
customers <- data.frame(
cust_id = c(1, 2, 4),
name = c("Alice", "Bob", "Dave")
)
merge(orders, customers, by = "cust_id") # inner join
merge(orders, customers, by = "cust_id", all.x = TRUE) # left join
merge(orders, customers, by = "cust_id", all = TRUE) # full outer
# apply family
m <- matrix(1:12, nrow = 3)
apply(m, 1, sum) # row sums -> length 3
apply(m, 2, sum) # column sums -> length 4
df <- data.frame(a = c(1,2,3), b = c(4,5,6), c = c(7,8,9))
sapply(df, mean) # mean of each column -> named numeric vector
lapply(df, range) # min/max of each column -> named list
# tapply — grouped summary
scores <- c(85, 90, 78, 92, 88, 76)
groups <- c("A", "B", "A", "B", "A", "B")
tapply(scores, groups, mean) # mean score per group
# do.call — assemble a data frame from a list of rows
rows <- list(
data.frame(x = 1, y = "one"),
data.frame(x = 2, y = "two")
)
do.call(rbind, rows)