Search code examples
rtidyversereadfile

How can I do to arrange different dataframes in R (one beneath other) without caring about matching parameters?


I have different excel files which I want them to be arranged in certain way: The data correspond to different equipment parameters,so, each excel file contains rows with data concerning to records per day per month which means that each data row has the data for a day in that month (date in the first column for all the records), then each file has for instance 31 rows for january, 28 for february and so on. The thing is that those are separated files, and I want to create a single dataframe with all the data arranged in the same order(from january to december, considering each date of the day as well) one set of data beneath en ensuing set of data. I tried to do the next.

Obtain a list for each file of data

tojoin = lapply(mis_archivos, function(i){
  x=read_excel(i, sheet = 1)
  x$file = i
  x
})

then I tried to join the list in a dataframe with this

join = do.call("rbind.data.frame", tojoin)'

And I got messages like this

Error in rbind.data.frame(list(Date = c(1388534400, 1388620800, 1388707200,  : 
  numbers of columns of arguments do not match

Even If I tried to convert a dataframe directly I got this

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 31, 28

hope have being clear enough. Please Help!!


Solution

  • rbind works only if the dimensions and the column names are the same. Here, we can use bind_rows

    library(dplyr)
    bind_rows(tojoin, .id = 'file')
    

    Or with rbindlist

    library(data.table)
    rbindlist(tojoin, idcol = 'file')