How to Append Data Frames in R

When it comes to appending data frames, the rbind() and cbind() function comes to mind because they can concatenate the data frames horizontally and vertically. In this example, we will see how to use the rbind() function to append data frames.

Append Data Frames in R

To append data frames in R, use the rbind() function. The rbind() is a built-in R function that can combine several vectors, matrices, and/or data frames by rows.

To join two data frames (datasets) vertically, use the rbind() function.

Syntax

rbind(a, b)

Parameters

The is input data.

The b is the data that needs to be binded.

Implementation of appending data frames in R

R provides many built-in datasets, and “cars” is one of them. So let’s use that data frame to illustrate how to append data frames. For example, to create two data frames from the cars dataset, use the head() and tail() functions.

data("cars")

df1 <- head(cars)
df2 <- tail(cars)

cat("First Data Frame: ", "\n")
df1
cat("Second Data Frame: ", "\n")
df2

See the below output.

First Data Frame:
   speed   dist
1    4      2
2    4     10
3    7      4
4    7     22
5    8     16
6    9     10
   Second   Data Frame:
   speed   dist
45   23     54
46   24     70
47   24     92
48   24     93
49   24    120
50   25     85

To append the df2 data frame to df1, use the rbind() function.

data("cars")

df1 <- head(cars)
df2 <- tail(cars)

cat("First Data Frame: ", "\n")
df1
cat("Second Data Frame: ", "\n")
df2

appendedDf <- rbind(df1, df2)
cat("The appended data frame", "\n")
appendedDf

Output

First Data Frame:
  speed   dist
1   4      2
2   4     10
3   7      4
4   7     22
5   8     16
6   9     10
Second Data Frame:
   speed  dist
45  23     54
46  24     70
47  24     92
48  24     93
49  24    120
50  25     85
The appended data frame
   speed  dist
1    4     2
2    4    10
3    7     4
4    7    22
5    8    16
6    9    10
45  23    54
46  24    70
47  24    92
48  24    93
49  24   120
50  25    85

From the output, you can see that the df2 has been appended to df1. This is because both data frames are concatenated vertically.

Appending a Column to data frame

To append a column to the data frame in R, use the symbol $ to append the data frame variable and add a column. Then, create a new vector that acts as a column and add that vector to the existing data frame in a column.

data("cars")

df <- head(cars)

cat("The Data Frame: ", "\n")
df

mileage <- c(70, 80, 90, 100, 110, 120)
cat("After Appendung a new column Data Frame: ", "\n")

df$mileage <- mileage
df

Output

The Data Frame:
  speed   dist
1   4      2
2   4     10
3   7      4
4   7     22
5   8     16
6   9     10
After Appendung a new column Data Frame:
  speed   dist   mileage
1   4      2      70
2   4     10      80
3   7      4      90
4   7     22     100
5   8     16     110
6   9     10     120

In this example, we first defined and printed the data frame and then defined a vector that will act as a column when we will add to the data frame, and using the $ symbol, and we appended a column to the data frame.

That’s it. Have a great day!

Leave a Comment