Built with Tutify.io

    AIStudyLabs

    R Append Single or Multiple Rows to Data Frame

    R Append Single or Multiple Rows to Data Frame

    To append a single row or multiple rows to the R data frame use either rbin(), add_row() from the tidyverse package, or rows_insert() from the dplyr package. In this article, I will use these methods to explain appending rows to the existing data frame.

    <a id="h-1-quick-example-of-appending-rows-to-data-frame"></a>

    1. Quick Example of Appending Rows to Data Frame

    Below are some examples of appending rows to a data frame.

    
    #append row to end of data frame 
    #using rbind()
    vecRow <- c(15, 'Kumar')
    df1 <- rbind(df1,vecRow)
    
    #using nrow()
    df1[nrow(df1) + 1,] = vecRow
    
    #using tidyverse to add row
    library(tidyverse)
    df1 <- df1 %>% add_row(id='20',
                           name='Ann',
                           .before=2)
    
    library(dplyr)
    df2 = data.frame(id = '25', name = 'Lyn')
    df2 <- df1 %>% 
      rows_insert(df2)
    

    Let’s create an R DataFrame, run these examples, and explore the output. If you already have data in CSV you can easily import CSV files to R DataFrame. Also, refer to Import Excel File into R.

    
    # Create DataFrame
    df1 <- data.frame(id = c(10,11),
                      name = c('sai','ram'))
    df1
    

    Yields below output.

    2. Using rbin() to Append Rows to R Data Frame

    Use the rbin() function from the R base package to append rows to the data frame in R. This function appends all rows at the end of the data frame and returns the result in a new data frame. This function requires the data frame and the row you are appending to have the same columns.

    In the below example, I have assigned the result to the data frame df1 object.

    
    #append row to end of data frame 
    #using rbind()
    vecRow <- c(15, 'Kumar')
    df1 <- rbind(df1,vecRow)
    df1
    

    Yields below output.

    Alternatively, you can also use the nrow() function. This approach updates the data frame with the row you inserted.

    3. Using tidyverse Package to Append Rows

    The add_row() function from the tidyverse package can be used to append rows to an R data frame. This function enables you to add a row at a specific index. The example below shows how to insert a row at the second position.

    To use this, you have to the install package tidyverse first using install.packages('tidyverse') and load it using library(tidyverse).

    
    # Using tidyverse to add row
    library(tidyverse)
    df1 <- df1 %>% add_row(id='20',
                           name='Ann',
                           .before=2)
    df1
    

    4. Using dplyr Package to Append Rows

    Finally, let's use the rows_insert() function from the dplyr package to append a row to the R data frame. In order to use this, you have to install package dplyr first using install.packages('dplyr') and load it using library(dplyr). In the below example, %>% is a pipe or infix operator.

    
    # Using dplyr
    library(dplyr)
    df2 = data.frame(id = '25', name = 'Lyn')
    df2 <- df1 %>% 
      rows_insert(df2)
    df2
    

    5. Append Multiple Rows to Data Frame

    By using the approaches explained above you can also append multiple rows to the R data frame. Below is an example.

    
    # Create DataFrame
    df1 <- data.frame(id = c(10,11),
                      name = c('sai','ram'))
    df1
    
    # Create DataFrame
    df2 <- data.frame(id = c(16,17),
                      name = c('Don','Lin'))
    
    df2
    
    #Append multiple rows to the data frame
    #using rbind()
    df3 <- rbind(df1,df2)
    df3
    

    For other approaches refer to append data frames in R, here I have covered several examples of appending data frames into a single data frame.

    6. Conclusion

    In this article, you have learned how to append single row and multiple rows to the R data frame by using rbin() function from the R base package, add_row() from the tidyverse package, and rows_insert() from the dplyr package.

    Related Articles