The data.table :: fread function is an optimized version of read.table. The skip option allows you to include a string that marks the beginning of the file. The function is also quite efficient in detecting tabs automatically, which is very useful if your files follow different standards. See the example:
library(data.table)
dfEx <- fread(
input = '# um comentário marcado com "hashtag"
data de criação: 12/10/2018
1 2 3
nome idade escolaridade
joao 10 6ano
Bia 20 faculdade',
skip = 'nome'
)
> dfEx
nome idade escolaridade
1: joao 10 6ano
2: Bia 20 faculdade
The default of fread is to generate an object of data.table and data.frame classes; you can change this with the data.table = FALSE
option.
To read multiple files at once, applying the fread (or read.table, etc) function on the list is more efficient than using a loop:
listaArquivos <- list.files(pattern = '.csv$')
# 0 $ indica para selecionar nomes que terminam com .csv
if( length(listaArquivos) ) listaDados <- lapply(listaArquivos, fread, skip = 'nome')
This will generate a list where each element is a data.table (or data.frame). You can merge everything by using the data.table :: rbindlist:
dados <- rbindlist(listaDados)