How to Append Data Frames in R

Here are two ways to append data frames in R.

  1. Using rbind() function
  2. Using nrow() function

Method 1: Using the rbind() function

The easiest way to append data frames in R is to the “rbind()” function. The “rbind()” function appends all records from the second data frame at the end of the first data frame.

Syntax

rbind(a, b)

Parameters

  1. The is input data.
  2. The b is the data that needs to be binded.

Example 1: Appending data frames in R

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

Example 2: Append a Column to a 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.

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

Example 3: Append Row to Data Frame

Use the rbind() function to combine the data frame with the row to be appended.

# Create a data frame.
df <- data.frame(
  Name = c("Ankit", "Rushabh", "Dhaval"),
  Age = c(20, 25, 28)
)

# Append a row to the data frame.
new_row <- data.frame(
  Name = "Krunal",
  Age = 30
)

# Combine the data frame with the row to be appended.
df <- rbind(df, new_row)

# Print the data frame.
print(df)

Output

   Name   Age
1  Ankit   20
2  Rushabh 25
3  Dhaval  28
4  Krunal  30

Method 2: Using the nrow() function

You can use the nrow() function to append a row to the end of a given data frame.

The expression for appending data frames is df[nrow(df) + 1,] = c(value1, value2, …).

Syntax

nrow(obj)

Parameters

obj: It can be a vector, array, data frame, NULL, or matrix.

Example

df <- data.frame(
        var1 = c(11, 21, 19),
        var2 = c(18, 46, 29),
        var3 = c(20, 23, 7)
      )
df

cat("After appending a row using rnow() function", "\n")

df[nrow(df) + 1, ] <- c(2, 52, 4)
df

Output

  var1  var2  var3
1  11    18    20
2  21    46    23
3  19    29    7
After appending a row using rnow() function
  var1  var2  var3
1  11   18    20
2  21   46    23
3  19   29    7
4   2   52    4

That’s it.

Leave a Comment