I trying to read the number of rows and columns in several csv files inside a folder.
My code read all files but showed 0 row and 0 column.
files_map <- "C:/Users/Windows 10/Desktop/dados/planilhas LLS"
files <- list.files(full.names = F)
library(data.table)
output <- data.table::rbindlist(lapply(files, function(file) {
dt <- data.table::fread(paste(files_map, file, sep = " "))
list("number_of_cols" = ncol(dt), "number_of_rows" = nrow(dt), "name_of_file" = file)}))
How could I solve this?
Thanks
CodePudding user response:
I made a test on my computer, slightly changing your files and this produces a correct output. You need to change paste
to paste0
because you don't want spaces in your filenames, and then add a trailing /
.
library(data.table)
setwd("Desktop/")
## make up some random files
fwrite(mtcars, "test_a")
fwrite(mtcars, "test_b")
fwrite(mtcars, "test_c")
files_map <- "~/Desktop"
output <- data.table::rbindlist(lapply(files, function(file) {
dt <- data.table::fread(paste0(files_map, "/", file))
list("number_of_cols" = ncol(dt), "number_of_rows" = nrow(dt), "name_of_file" = file)
})
)
number_of_cols number_of_rows name_of_file
1: 11 32 test_a
2: 11 32 test_b
3: 11 32 test_c