Как переименовать имена столбцов, используя мой код с purr :: map и colnames - PullRequest
0 голосов
/ 01 октября 2019

Пожалуйста, исправьте мой код. Если я запускаю код без части 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"))

Ответы [ 2 ]

1 голос
/ 01 октября 2019

Следующие коды должны работать. См. Использование%>% pipe и точечная (.) Нотация

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

Для простоты:

Переименование радужной оболочки в конвейере

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
0 голосов
/ 02 октября 2019

Попробуйте использовать rename_all. Также не нужно дважды вызывать filter, вы можете комбинировать условия с оператором ИЛИ (|) и использовать его один раз. Предполагая, что каждый кадр данных в lst2 имеет такое же количество столбцов, что и имена, переданные в rename_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")))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...