I have 60 txt files all with the same number of columns. The files do not have header (name of the columns).
How can I put all of them together in a file just below one another in R.
I have 60 txt files all with the same number of columns. The files do not have header (name of the columns).
How can I put all of them together in a file just below one another in R.
The code below should be tailored to your specific needs, but it should give you a good starting point. Attention: R must be set to the directory where the .txt files are located:
lista <- list.files(pattern="txt")
arquivos <- lapply(lista, function(x) read.table(x, header=FALSE, sep=" "))
dados <- do.call("rbind", arquivos)
Let's break it down into parts:
list.files
returns all files in the directory according to a pattern. In case, everyone who has txt in the title
lapply
applies the read.table
function to each filename present in lista
, whereas the files do not have a header and the column separator is a blank space. In your case, this tab might be a comma, a semicolon, or tab stops. Adjust the code according to your needs. do.call
applies the rbind
function, responsible for joining data frames according one below the other, in the data that was stored in arquivos