I have two folders each with hundreds of CSVs and I want to merge them all in one data frame. I have used the following:
tbl <-
list.files(path = c("./reports_0", "./reports_1"),
pattern = "*.csv",
full.names = T) %>%
map_dfr(~read_csv(., col_types = cols(.default = "c")))
Now I realized that some of those CSVs have their column name as Firmware Version and some as Firmware version (upper and lowercase).
I would like to specify that those are the same and can be combined in one called Firmware Version.
the
by =
does not work and I could not find a solution.
Hope there is someone that can help, thanks!
EDIT
My workaround is:
tbl <- tbl %>%
unite(`Firmware Version`, `Firmware version`, na.rm = T) %>%
mutate(`Firmware Version` = replace(`Firmware Version`, `Firmware Version`=="", NA_character_))
However, I still wonder whether there is a nicer, more straightforward way.
you could use janitor::make_clean_names()
to convert columnnames to the same format (for example camelCase), and then rowbind.
for example:
library(data.table)
library(janitor)
ftr <- list.files(path = c("./reports_0", "./reports_1"),
pattern = ".*\\.csv$",
names = TRUE)
DT <- rbindlist(
lapply(ftr, function(x) {
tempDT <- fread(x)
setnames(tempDT, names(tempDT), janitor::make_clean_names(names(tempDT)))
return(tempDT)
}), use.names = TRUE, fill = TRUE)
proof of concept
convert names to snake_case
> janitor::make_clean_names("Firmware Version")
[1] "firmware_version"
> janitor::make_clean_names("Firmware version")
[1] "firmware_version"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.