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 automatic detection of separators, which is quite useful if your files follow different patterns. Take 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 fread is to generate an object from the data.table and data.frame classes; you can change this with the option data.table = FALSE.
To read multiple files at once, applying the fread function (or read.table, etc.) over 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 in which each element is a data.table (or data.frame). You can merge everything using the data.table:rbindlist function:
dados <- rbindlist(listaDados)
is.empty(filenames)==FALSEnot a good idea. Use!is.empty(filenames).– Rui Barradas
what’s the difference ?
– Brenda Xavier
You must not test
variavel == TRUEorvariavel == FALSEbecause the variable already isTRUEorFALSE. Sufficeif(variavel)in the caseTRUEor denyif(!variavel)in the caseFALSE.– Rui Barradas
obgda, I’ll make that change ^^
– Brenda Xavier