如何使用带有purr::map和colnames的代码重命名列名
请更正我的密码。如果我在没有colnames部分的情况下运行代码,它会运行得很好,但是我需要重命名这些数据集中的所有列。我不确定这为什么不能与“colnames”函数一起使用,我相信我没有正确地使用它。我不知道在什么时候可以相应地重命名列名 请注意,我正在遍历多个数据集如何使用带有purr::map和colnames的代码重命名列名,r,R,请更正我的密码。如果我在没有colnames部分的情况下运行代码,它会运行得很好,但是我需要重命名这些数据集中的所有列。我不确定这为什么不能与“colnames”函数一起使用,我相信我没有正确地使用它。我不知道在什么时候可以相应地重命名列名 请注意,我正在遍历多个数据集 ##Extract all xlsx files in the directory stated file_path <- "data_raw/Result_Summary/" xlsx_file_names <
##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
##提取指定目录中的所有xlsx文件
文件路径%
[str_detect(,“.xlsx”)]
xlsx_文件名%>%
映射(函数(文件名){遍历每个文件名
分配(x=str_remove(文件名“.xlsx”),#删除文件扩展名“.xlsx”
value=read\u excel(粘贴0(文件路径,文件名),sheet=“Results Summary”,range=c(“B11:V120”),
col_types=c(“文本”,
“猜猜看”,
“猜猜看”,
“数字”,
“数字”,
“数字”,
“数字”,
“数字”,
“数字”,
“数字”,
“猜猜看”,
“数字”,
“数字”,
“猜猜看”,
“数字”,
“数字”,
“猜猜看”,
“数字”,
“数字”,
“猜猜看”,
“数字”),
envir=.GlobalEnv)
})
rm(文件路径、xlsx文件名)
##将全局环境中的所有数据集合并到一个列表中##
lst2%
筛选器(!活动==“总计”)%>%
colnames(lst2)%filter(!is.na(活动))%%>%filter(!Campaign==“Total”)%%>%:
找不到对象“.x”
这是对代码的更新,感谢@Ronak Shah的输入。这是他的代码,我只是改变了顺序,因为这使代码运行没有错误。所以,我将下面的代码包括在下面,供一直跟踪的人使用。
文件路径%
[str_detect(,“.xlsx”)]
xlsx_文件名%>%
映射(函数(文件名){遍历每个文件名
分配(x=str_remove(文件名“.xlsx”),#删除文件扩展名“.xlsx”
value=read\u excel(粘贴0(文件路径,文件名),sheet=“Results Summary”,range=c(“B11:V120”),
col_types=c(“文本”,
“猜猜看”,
“猜猜看”,
“数字”,
“数字”,
“数字”,
“数字”,
“数字”,
“数字”,
“数字”,
“猜猜看”,
“数字”,
“数字”,
“猜猜看”,
“数字”,
“数字”,
“猜猜看”,
“数字”,
“数字”,
“猜猜看”,
“数字”),
envir=.GlobalEnv)
})
rm(文件路径、xlsx文件名)
##将全局环境中的所有数据集合并到一个列表中##
lst2%
#“坏”))
过滤器(!is.na(活动))%>%
筛选器(!活动==“总计”))
以下代码应能正常工作。看
lst2%##对全局环境中的所有对象重复以下任务
过滤器(!is.na(活动))%>%
筛选器(!活动==“总计”)%>%
{
colnames(.)尝试使用rename\u all
。也无需调用filter
两次,您可以将条件与OR(
)运算符组合使用一次。假设lst2
中的每个数据帧的列数与在rename\u all
中传递的名称相同,我们可以这样做
library(dplyr)
purrr::map(lst2, ~ .x %>%
filter(!is.na(Campaign) | Campaign != "total") %>%
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")))
提前感谢。警告或错误消息是什么?在~.x%%>%筛选器(!is.na(活动))%%>%筛选器(!Campaign==“Total”)%%>%中出现错误。找不到对象“.x”,请将其添加到您的问题中。如果我在第二个“筛选器”之后取出代码包括管道操作符,然后用括号关闭最后的代码,代码运行正常,但一旦我添加colnames函数,它就没有任何效果,并出现上述错误。我无法帮助,因为您提供的示例不可复制。它不起作用
也没有帮助,如果您需要帮助,请粘贴错误消息。Pr提供一个可重复的示例。我不是故意粗鲁,我很感谢你的帮助。只是给你关于代码的反馈。我可以进一步解释这是否有帮助。感谢你的输入,你的建议奏效了。不过我不得不更改代码的顺序,因为当我按以前的顺序运行时,我收到了一条错误消息。
library(dplyr)
purrr::map(lst2, ~ .x %>%
filter(!is.na(Campaign) | Campaign != "total") %>%
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")))