How do I add a column to a data frame in R?

Adding a column to a data frame in R is a process of creating a new column and appending it to the existing data frame. This can be done by using the “cbind()” function, which combines the existing data frame with the new column. Alternatively, the “mutate()” function from the dplyr package can be used to add a new column by specifying the column name and the desired values. The new column can also be added by using the “insert()” function from the insert package, which allows for specifying the position of the new column within the data frame. By following these steps, a new column can be easily added to a data frame in R.

Add a Column to a Data Frame in R (With Examples)


There are three common ways to add a new column to a data frame in R:

1. Use the $ Operator

df$new <- c(3, 3, 6, 7, 8, 12)

2. Use Brackets

df['new'] <- c(3, 3, 6, 7, 8, 12)

3. Use Cbind

df_new <- cbind(df, new)

This tutorial provides examples of how to use each of these methods in practice using the following data frame:

#create data frame
df <- data.frame(a = c('A', 'B', 'C', 'D', 'E'),
                 b = c(45, 56, 54, 57, 59))

#view data frame
df

  a  b
1 A 45
2 B 56
3 C 54
4 D 57
5 E 59

Example 1: Use the $ Operator

The following code shows how to add a column to a data frame by using the $ operator:

#define new column to add
new <- c(3, 3, 6, 7, 8)

#add column called 'new'
df$new <- new

#view new data frame
df  a  b new
1 A 45   3
2 B 56   3
3 C 54   6
4 D 57   7
5 E 59   8

Example 2: Use Brackets

The following code shows how to add a column to a data frame by using brackets:

#define new column to add
new <- c(3, 3, 6, 7, 8)

#add column called 'new'
df['new'] <- new

#view new data frame
df 

  a  b new
1 A 45   3
2 B 56   3
3 C 54   6
4 D 57   7
5 E 59   8

Example 3: Use Cbind

The following code shows how to add a column to a data frame by using the function, which is short for column-bind:

#define new column to add
new <- c(3, 3, 6, 7, 8)

#add column called 'new'
df_new <- cbind(df, new)

#view new data frame
df_new

  a  b new
1 A 45   3
2 B 56   3
3 C 54   6
4 D 57   7
5 E 59   8

You can actually use the cbind function to add multiple new columns at once:

#define new columns to add
new1 <- c(3, 3, 6, 7, 8)
new2 <- c(13, 14, 16, 17, 20) 

#add columns called 'new1' and 'new2'
df_new <- cbind(df, new1, new2)

#view new data frame
df_new

  a  b new1 new2
1 A 45    3   13
2 B 56    3   14
3 C 54    6   16
4 D 57    7   17
5 E 59    8   20

Bonus: Set Column Names

After adding one or more columns to a data frame, you can use the colnames() function to specify the column names of the new data frame:

#create data frame
df <- data.frame(a = c('A', 'B', 'C', 'D', 'E'),
                 b = c(45, 56, 54, 57, 59),
                 new1 = c(3, 3, 6, 7, 8),
                 new2 = c(13, 14, 16, 17, 20))

#view data frame
df

  a  b new1 new2
1 A 45    3   13
2 B 56    3   14
3 C 54    6   16
4 D 57    7   17
5 E 59    8   20

#specify column names
colnames(df) <- c('a', 'b', 'c', 'd')

#view data frame
df

  a  b c  d
1 A 45 3 13
2 B 56 3 14
3 C 54 6 16
4 D 57 7 17
5 E 59 8 20

You can find more R tutorials .

x