简体   繁体   English

如何在 R 中以不同的 ID 名称加入多个 DF

[英]How to left join multiple DFs in R with different ID name

I have 15 CSVs on google drive, and I want to download them and left join them at the same time.我在谷歌驱动器上有 15 个 CSV,我想下载它们并同时加入它们。 Thirteen have the same ID variable (inegi), but two have different ID names (CVEGEO and id_mun).十三个具有相同的 ID 变量 (inegi),但两个具有不同的 ID 名称(CVEGEO 和 id_mun)。 How can I join the other two with different ID names in the columns?如何在列中加入其他两个具有不同 ID 名称的? Here is the code I currently have:这是我目前拥有的代码:

# Paquetes  ---------------------------------------------------------------
if(!require(pacman)) install.packages("pacman")
pacman::p_load(tidyverse, janitor, googledrive, 
               stringr, purrr)


# Download municipal Files -------------------------------------------------------------------
files_municipal <- drive_ls(as_id("IDOFGOOGLE")) %>% 
  filter(!str_detect(name, "cumulative-deaths|excess")) %>%
  arrange(name)

tmp <- paste(tempdir(), files_municipal$name, sep = "/")

walk2(files_municipal$id, tmp, ~ drive_download(as_id(.x), path = .y, overwrite = TRUE)) 

# Join --------------------------------------------------------------------
tempo <- map(tmp, read_csv) %>% 
  reduce(left_join, by = "inegi")  

I have the solution.我有解决办法。 I created a function at the beginning:我在开始时创建了一个函数:

# Paquetes  ---------------------------------------------------------------
if(!require(pacman)) install.packages("pacman")
pacman::p_load(tidyverse, janitor, googledrive, 
              stringr, purrr)

# Functions  ---------------------------------------------------------------
read_vars_mun <- function(df) {
 
 df <- read_csv(df) %>% 
   rename_with(
     ~ case_when(
       . == "CVEGEO" ~ "inegi",
       . == "id_mun" ~ "inegi",
       TRUE ~ .)) %>% 
   mutate(inegi = as.character(inegi)) 
}

# Download municipal Files -------------------------------------------------------------------
files_municipal <- drive_ls(as_id("IDOFGOOGLE")) %>% 
 filter(!str_detect(name, "cumulative-deaths|excess")) %>%
 arrange(name)

tmp <- paste(tempdir(), files_municipal$name, sep = "/")

walk2(files_municipal$id, tmp, ~ drive_download(as_id(.x), path = .y, overwrite = TRUE)) 

# Join --------------------------------------------------------------------
df <- map(tmp, read_vars_mun) %>% 
 reduce(left_join, by = "inegi")  

# Done. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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