I feel like this should be simple but I can't find the answer.
I have over a dozen reports that contain the same fields, but the fields have slightly different names in each report - 'total_spend' appears as 'ttl_spend' / 'total_spend' / 'totl_spnd' / etc.
I am combining them into a single dataset by wrapping all of them w bind_rows. Each dataset needs XYZ things edited and fixed. For each report in the bind, after doing whatever else I need to do, I want to apply a set of standardized names to the fields, then load and bind in the next report.
I am unable to find a command that works within a tidy/dplyr chain that takes values from a list and applies them as new column names.
What I'd like to do -
FieldList <- c('Name', 'OrderNumber', 'OrderDate', 'ShipDate', 'OrderTotal')
bind_rows(
read_csv(pathway/file1.csv) %>%
select(CustName, OrdNum, Date, Ship, Ttl) %>%
names(FieldList),
read_csv(pathway/file2.csv) %>%
select(NameCust, Order, OrdDate, ShipDt, OrdTotal) %>%
names(FieldList)) -> my_data
I have tried to use names(), colnames(), variations of rename(), and tried a few things within lapply. I cannot get it to work within the chain like I have above. I have to add each dataset to R Studio, then use names, then bind them. I want to do this within a single long bind_rows command. I know I can use rename(x1=x2) but I'd have to do that 8 times per dataset, for 15 datasets.
There's gotta be a way to update all names using a predefined list within the dplyr chain.
HELP.
read_csv(pathway/file1.csv) %>%
select(CustName, OrdNum, Date, Ship, Ttl) %>%
setNames(FieldList) %>%
bind_rows(read_csv(pathway/file2.csv) %>%
select(NameCust, Order, OrdDate, ShipDt, OrdTotal) %>%
setNames(FieldList)) -> my_data