Пожалуйста, исправьте мой код. Если я запускаю код без части colnames, он работает отлично, но мне нужно переименовать все столбцы в этих наборах данных. Я не уверен, почему это не работает с функцией "colnames", я считаю, что я не использую ее правильно. Я не знаю, в какой момент имена столбцов могут быть соответствующим образом переименованы.
Обратите внимание, что я перебираю несколько наборов данных
##Extract all xlsx files in the directory stated
file_path <- "data_raw/Result_Summary/"
xlsx_file_names <- file_path %>%
list.files() %>%
.[str_detect(., ".xlsx")]
xlsx_file_names %>%
purrr::map(function(file_name){ # iterate through each file name
assign(x = str_remove(file_name, ".xlsx"), # Remove file extension ".xlsx"
value = read_excel(paste0(file_path, file_name),sheet="Results Summary", range=c("B11:V120"),
col_types = c("text",
"guess",
"guess",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"guess",
"numeric",
"numeric",
"guess",
"numeric",
"numeric",
"guess",
"numeric",
"numeric",
"guess",
"numeric")),
envir = .GlobalEnv)
})
rm(file_path, xlsx_file_names)
##combine all datasets in the Global Enviroment to a list ##
lst2 <- mget(ls())
lst2 <- map(lst2, ~ .x %>% ## Repeat the task below on all objects in the global enviroment
filter(!is.na(Campaign)) %>%
filter(!Campaign == "Total")%>%
colnames(lst2) <- c("Campaign",
"Start_Date",
"End_Date",
"Total_Cost",
"Households",
"Deposit_Account",
"Deposit_Balance",
"Num_Loan",
"Loan_Bal",
"Direct_Response",
"Direct_Response_Rate",
"Direct_Balances",
"Direct_Margin",
"Direct_Margin_ROI",
"Direct_Acquisition_Cost_Account",
"Indirect_Response",
"Indirect_Response_Rate",
"Indirect_Balances",
"Indirect_Margin",
"Indirect_Margin_ROI",
"Indirect_Acquisition_Cost_Account"))
#"Bad"))
Error in ~.x %>% filter(!is.na(Campaign)) %>% filter(!Campaign == "Total") %>% :
object '.x' not found
This is an update on the code, thanks to @Ronak Shah for his input. It's his code, i only changed the order because that made the code to run without error.So, I'm including the code below for whoever has been following.
file_path <- "data_raw/Result_Summary/"
xlsx_file_names <- file_path %>%
list.files() %>%
.[str_detect(., ".xlsx")]
xlsx_file_names %>%
purrr::map(function(file_name){ # iterate through each file name
assign(x = str_remove(file_name, ".xlsx"), # Remove file extension ".xlsx"
value = read_excel(paste0(file_path, file_name),sheet="Results Summary", range=c("B11:V120"),
col_types = c("text",
"guess",
"guess",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"guess",
"numeric",
"numeric",
"guess",
"numeric",
"numeric",
"guess",
"numeric",
"numeric",
"guess",
"numeric")),
envir = .GlobalEnv)
})
rm(file_path, xlsx_file_names)
##combine all datasets in the Global Enviroment to a list ##
lst2 <- mget(ls())
lst2 <- map(lst2, ~ .x %>% ## Repeat the task below on all objects in the global enviroment
rename_all(~c("Campaign",
"Start_Date",
"End_Date",
"Total_Cost",
"Households",
"Deposit_Account",
"Deposit_Balance",
"Num_Loan",
"Loan_Bal",
"Direct_Response",
"Direct_Response_Rate",
"Direct_Balances",
"Direct_Margin",
"Direct_Margin_ROI",
"Direct_Acquisition_Cost_Account",
"Indirect_Response",
"Indirect_Response_Rate",
"Indirect_Balances",
"Indirect_Margin",
"Indirect_Margin_ROI",
"Indirect_Acquisition_Cost_Account")) %>%
#"Bad"))
filter(!is.na(Campaign)) %>%
filter(!Campaign == "Total"))