import multiple file txt and create one data frame

Goodmorning everyone,

I introduce myself: I am Soda and I have recently started using Rstudio! I appeal to you to solve a problem (certainly simple for an expert) that is complicated for a neophyte.

in a folder (dataR) I have about 800 files in format (.txt and .dat) named with the same order "name_surname_12345678" which contain data organized with headers (all the same). The numbers (12345678) instead are the dates organized in YYYYMMDD.

What I need to do is concatenate all this data in order, into one file. Who can help me ?

I have tried in different ways with the guides found online but unfortunately I cannot find a solution

thanks a lot

if dataR contains only files that are csv's with the same columns then this will work.

(files_to_read <- list.files(path="dataR"))

read_a_file <- function(x){
  read.csv(file = file.path("dataR",x)
         ,header = TRUE) %>% mutate(filename=x) 


How beautiful! now I finally managed to read all the files and insert them into a dataframe! and for that thank you very much.

However, I have another problem: basically when I view the file in the console (or by clicking on the name of the dataframe in the global environment) the data is disorganized.

That is, they are not displayed according to the variables (listed in the column - which are 18 in total), in fact only 3 variables are recognized. How can I do to read the file correctly?

I try to attach a jpeg so you can see how it is structured


well, they are certainly not comma seperated fles,... are they space separated ?
and given that, the headers feature non standard characters which might cause some pain also.

If you can go back to the system that generated them , and turn them into genuine csv's that would be likely optimal. otherwise we can work with what we have and try to correct for these issues.

For further help, it wont be sufficient to see an image of your example file, rather you should provide the file to the forum as text.

you can format the display on the forum by using triple backticks

like this


I am writing to inform you that I solved the problem by introducing the "space" separator in file.path, as follows:

read.csv(file =file.path("directory",x),header = TRUE, sep = " ") %>% mutate(filename=x)

Now i can get the tables sorted according to all the variables in the column.

Thank you very much for your "fundamental" contribution.

I hope I can return the favor


1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.