Search code examples
rdata-bindingdatahandler

how to bind the rows of tibbles together using the bind_row(function"


So it has been several years since I have done anything With R studio and I cannot remember a thing about it..

I have currently read 12 different CSV files into the console using the read_csv() function for each one respectively and this has given my twelve tibbles.

Now I need to combine these together using the bind_rows() function but I have no idea how to do this. Every attempt I have made has given the error code "argument 1 must have names"

Any help for a novice would be much appreciated. I have copied 2 parts of my code bellow.

Cheers

Read the Hereford crime data into R using function read_csv() in package readr

read_csv("2019-01-west-mercia-street.csv")

read_csv("2019-02-west-mercia-street.csv")

bind data frames

bind_rows("2019-01-west-mercia-street.csv", 2019-02-west-mercia-street.csv")

Error: Argument 1 must have names


Solution

  • Thats because you try to bind two strings together, where the function expects two data.frames.

    library(readr)
    library(dplyr)
    
    df1 <- read_csv("2019-01-west-mercia-street.csv")
    df2 <- read_csv("2019-02-west-mercia-street.csv")
    
    bind_rows(df1, df2)
    

    But if I were you, I'd import them all at once with import_list() from the rio package. Lets say you get all the 12 file names of the csv's with

    files <- dir(pattern = "csv$")
    

    Then you could easily import and row bind them with

    library(rio)
    df <- import_list(files, rbind = TRUE, setclass = "tbl")
    

    where setclass sets the output to a tibble.