简体   繁体   中英

How to rename column names using my code with purrr::map and colnames

Please correct my code. If i run the code without the colnames part it runs perfectly but i need to rename all the columns in these datasets. I'm not sure why this isn't working with the "colnames" function, I believe I'm not using it correctly. I don't know at what point the column names can be renamed accordingly.

Please, note that I'm looping through multiple datasets

##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"))

The following codes should work. See Using the %>% pipe, and dot (.) notation

lst2 <- map(lst2, ~ .x %>% ## Repeat the task below on all objects in the global enviroment
                filter(!is.na(Campaign)) %>% 
                filter(!Campaign == "Total")%>% 
                {
                    colnames(.) <- 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")
                    return(.)
                })

# The piped version should be equivalent to 

lst2 <- map(lst2, function(x){
    x_filtered <- x %>% 
        filter(!is.na(Campaign)) %>% 
        filter(!Campaign == "Total")
    colnames(x_filtered) <-  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")

    return(x_filtered)
})

For simplicity:

Rename iris in a pipeline

iris %>%
    {
        colnames(.) <- c("a","b","c","d","e")
        return(.)
    } %>%
    head()
    a   b   c   d      e
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa

Try using rename_all . Also no need to call filter twice, you can combine the conditions with OR ( | ) operator and use it once. Assuming each dataframe in lst2 has same number of columns as the names passed in rename_all , we can do

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")))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM