Add Column to R Data Frame
To add a new column to an R data frame, assign a vector to a new column name with the dollar-sign operator $.
mydataframe$newcolumn <- vector_name
The number of values assigned to the new column should normally match the number of rows in the data frame. A single value is recycled to every row, but an incompatible vector length produces an error.
In the syntax above:
mydataframeis the data frame to which the new column is added.newcolumnis the name of the new column.vector_namecontains the values to store in the new column.
Add a Column with the Dollar-Sign Operator
The $ operator is the most direct option when the new column name is written explicitly in the code.
Example 1 – Add Column to R Data Frame
In this example, we create a data frame named DF1 and add a fourth column named V4.
> DF1 = data.frame(V1= c(1, 5, 14, 23, 54), V2= c(9, 15, 85, 3, 42), V3= c(9, 7, 42, 87, 16))
> DF1
V1 V2 V3
1 1 9 9
2 5 15 7
3 14 85 42
4 23 3 87
5 54 42 16
> DF1$V4 = c(55, 66, 85, 12, 21)
> DF1
V1 V2 V3 V4
1 1 9 9 55
2 5 15 7 66
3 14 85 42 85
4 23 3 87 12
5 54 42 16 21
>
Column V4 is added at the end of the data frame because its vector has five values, matching the five rows in DF1.
Example 2 – Add a Shorter Vector as a Column
In this example, the new vector has four values while the data frame has five rows.
> DF1 = data.frame(V1= c(1, 5, 14, 23, 54), V2= c(9, 15, 85, 3, 42), V3= c(9, 7, 42, 87, 16))
> DF1
V1 V2 V3
1 1 9 9
2 5 15 7
3 14 85 42
4 23 3 87
5 54 42 16
> DF1$V4 = c(55, 66, 85, 12)
Error in `$<-.data.frame`(`*tmp*`, V4, value = c(55, 66, 85, 12)) :
replacement has 4 rows, data has 5
>
R reports replacement has 4 rows, data has 5 because the assigned vector length is not compatible with the number of data-frame rows.
Add a Constant or Empty Column in R
Assigning one value creates a column in which that value is repeated for every row. This is useful for flags, categories, and default values.
employees <- data.frame(
name = c("Asha", "Bala", "Charan"),
salary = c(45000, 52000, 48000)
)
employees$status <- "active"
employees$bonus <- NA_real_
employees
name salary status bonus
1 Asha 45000 active NA
2 Bala 52000 active NA
3 Charan 48000 active NA
NA_real_ creates a missing-value column intended for numeric data. Other typed missing values include NA_integer_, NA_character_, and NA for logical data.
Create a New Column from Existing R Columns
A new column can be calculated from one or more existing columns. R performs the operation row by row when the referenced columns are vectors of equal length.
sales <- data.frame(
product = c("Pen", "Book", "Bag"),
price = c(10, 80, 500),
quantity = c(4, 2, 1)
)
sales$total <- sales$price * sales$quantity
sales
product price quantity total
1 Pen 10 4 40
2 Book 80 2 160
3 Bag 500 1 500
Create a Conditional Column with ifelse()
Use ifelse() when the new value depends on a condition evaluated for each row.
students <- data.frame(
name = c("Anu", "Dev", "Mira"),
marks = c(72, 38, 85)
)
students$result <- ifelse(students$marks >= 40, "Pass", "Fail")
students
name marks result
1 Anu 72 Pass
2 Dev 38 Fail
3 Mira 85 Pass
Add a Column Using a Variable Column Name
When the column name is stored in a variable, use double brackets [[ ]]. Writing data$name would create a literal column named name, not a column whose name comes from the variable.
column_name <- "department"
employees[[column_name]] <- c("Sales", "HR", "IT")
employees
Insert a Column at a Specific Position
Base R normally appends a new column at the end. To insert it between existing columns, combine selected parts of the data frame with the new vector.
df <- data.frame(
id = 1:3,
name = c("A", "B", "C"),
score = c(70, 82, 91)
)
grade <- c("B", "A", "A")
df <- data.frame(df[1:2], grade = grade, df[3])
df
id name grade score
1 1 A B 70
2 2 B A 82
3 3 C A 91
With the tibble package, add_column() can place a column by using .before or .after.
library(tibble)
df <- add_column(df, city = c("Pune", "Delhi", "Kochi"), .after = "name")
Add Columns with cbind() and transform()
Base R also provides cbind() and transform(). Both return a data frame containing the added column, so assign the result back to an object.
df <- cbind(df, age = c(21, 24, 20))
df <- transform(
df,
passed = score >= 40
)
Use cbind() carefully because it binds objects by row position. Confirm that the new vector belongs to the same row order as the data frame.
Add a Column with dplyr mutate()
In a dplyr workflow, mutate() adds or replaces columns and makes it convenient to refer to existing columns without repeating the data-frame name.
library(dplyr)
sales <- sales |>
mutate(
total = price * quantity,
size = if_else(total >= 200, "Large", "Small")
)
mutate() can add several columns in one call. Later expressions in the same call may use columns created earlier in that call.
Avoid Length and Row-Order Errors
- Check the row count with
nrow(df)before assigning an external vector. - Check the vector length with
length(values). - Do not assume two separately sorted objects still have the same row order.
- When values are identified by a key, use a join or
match()instead of relying only on position. - Use typed missing values when the intended column type matters.
stopifnot(length(values) == nrow(df))
df$new_column <- values
Frequently Asked Questions about Adding R Data-Frame Columns
How do I add an empty column to a data frame in R?
Assign NA or a typed missing value to the new column. For example, df$amount <- NA_real_ creates an empty numeric column with one missing value for every row.
How do I add a column based on other columns in R?
Assign the calculation directly, such as df$total <- df$price * df$quantity. In dplyr, use mutate(df, total = price * quantity).
How do I add a column in a specific place in R?
In base R, rebuild the data frame by combining the columns in the required order. With tibble, use add_column() and specify .before or .after.
How do I use a variable as the new column name?
Store the name in a character variable and assign with double brackets: df[[column_name]] <- values.
Why does R say replacement has more or fewer rows than the data?
The assigned vector length is incompatible with the number of rows. Supply one value for recycling or a vector whose length matches nrow(df).
Editorial QA Checklist for This R Column Tutorial
- Verify that each assigned vector has either one value or the same length as the example data frame.
- Confirm that output tables use the same row order and values as their corresponding R code.
- Check that dynamic column-name examples use
[[ ]]rather than a literal$namereference. - Confirm that specific-position examples place the new column exactly where the text states.
- Run base R and dplyr examples in a clean R session with the required package loaded.
TutorialKart.com