How to Add Row to data.table in R


Often you may want to add one or more new rows to a data.table in R.

You can use the following methods to do so:

Method 1: Add One New Row to data.table in R

rbind(dt, list('C', 'G', 90, 34))

This particular example will add one new row to the end of the data.table named dt, using the values specified in the list() function.

Method 2: Add Multiple New Rows to data.table in R

rbind(dt, list('C', 'G', 90, 34), list('A', 'F', 95, 45))

This particular example will add two new rows  to the end of the data.table named dt, using the values specified in each of the list() functions.

Note that both of these methods use the rbind() function, which is designed to “bind” new rows to a data frame or data.table in R.

The following example shows how to use each of these methods in practice with the following data.table in R:

library(data.table)

#create data table
dt <- data.table(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 position=c('G', 'G', 'F', 'F', 'G', 'G', 'F', 'F'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 45, 35, 34, 45, 28, 31))

#view data table
dt

   team position points assists
1:    A        G     99      22
2:    A        G     68      28
3:    A        F     86      45
4:    A        F     88      35
5:    B        G     95      34
6:    B        G     74      45
7:    B        F     78      28
8:    B        F     93      31

The data.table contains the following columns:

  • team: The team name a player belongs to
  • position: The position of the player
  • points: The total points scored by the player
  • assists: The total assists made by the player

Example 1: Add One New Row to data.table in R

Suppose that we would like to add one new row to the end of the data.table that contains information about the team, position, points and assists for a new basketball player.

We can use the following syntax to do so:

library(data.table)

#add one new row to end of data.table
rbind(dt, list('C', 'G', 90, 34)) 

   team position points assists
1:    A        G     99      22
2:    A        G     68      28
3:    A        F     86      45
4:    A        F     88      35
5:    B        G     95      34
6:    B        G     74      45
7:    B        F     78      28
8:    B        F     93      31
9:    C        G     90      34

Notice that one new row has been added to the end of the data.table with the exact values that we specified in the list() function within the rbind() function.

Note: The number of values in the list() function should match the number of existing columns in the data.table.

Example 2: Add Multiple New Rows to data.table in R

Suppose that we would like to add one two new rows to the end of the data.table that contains information about two new basketball players.

We can use the following syntax to do so:

library(data.table)

#add two new rows to end of data.table
rbind(dt, list('C', 'G', 90, 34), list('A', 'F', 95, 45))

    team position points assists
 1:    A        G     99      22
 2:    A        G     68      28
 3:    A        F     86      45
 4:    A        F     88      35
 5:    B        G     95      34
 6:    B        G     74      45
 7:    B        F     78      28
 8:    B        F     93      31
 9:    C        G     90      34
10:    A        F     95      45

Notice that two new rows have been added to the end of the data.table with the values that we specified.

Additional Resource

The following tutorials explain how to perform other common tasks in R:

How to Filter a data.table in R
How to Sort a data.table in R
How to Group data.table by Multiple Columns in R
How to Use dcast Function from data.table in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *