How to Add Calculated Columns to Data Frames in R
Add calculated columns to your data frame to derive new values from existing columns — total price from unit price and quantity, a discount flag from order size, or a normalized score from raw measurements. R is vectorized, so you operate on whole columns at once without row-by-row loops. With dplyr’s mutate(), you can reference a column you just created within the same call, chaining dependent calculations cleanly in one pipeline. This approach keeps your data transformation logic readable and avoids cluttering the workspace with intermediate variables. The result is a self-contained block of code that reads from top to bottom like a recipe.
library(dplyr)
df <- df %>%
mutate(
total_price = price * quantity,
discount = ifelse(total_price > 100, 0.1, 0),
final_price = total_price * (1 - discount)
)
In base R, assign directly to the column name:
df$total_price <- df$price * df$quantity
For data.table, the := operator modifies the table in place without copying, which matters for large data:
library(data.table)
dt[, total_price := price * quantity]
Use ifelse() or dplyr::case_when() when the formula depends on a condition. case_when() is cleaner than nested ifelse() calls when you have more than two cases.