使用R中更改的数据和变量名称运行相同的代码

时间:2019-05-28 17:46:30

标签: r function loops

我需要为3个不同的数据集运行非常相似的代码。我当前的代码如下:

## data a
a_dat2 <- merge(a_dat, zip, by = "zip", all.x = T)
a_dat2 <- a_dat2 %>%
group_by(zip) %>%
summarize(dist_a_min = min(dist))
## data b
b_dat2 <- merge(b_dat, zip, by = "zip", all.x = T)
    b_dat2 <- b_dat2 %>%
     group_by(zip) %>%
summarize(dist_b_min = min(dist))
## data c
c_dat2 <- merge(c_dat, zip, by = "zip", all.x = T)
    c_dat2 <- c_dat2 %>%
     group_by(zip) %>%
summarize(dist_c_min = min(dist))

3个数据集的代码相同,除了数据名称不同:a_datb_datc_dat。变量名称dist也有所不同:dist_a_mindist_b_mindist_c_min。可以使用什么函数/循环来缩短代码,这样就不必分别复制和粘贴每个数据集了?

1 个答案:

答案 0 :(得分:3)

一种选择是将元素与list放在mget中,通过list遍历imap,将'?left_join)与' zip”数据集,按“ zip”分组,并在基于标识符名称子字符串的列名称创建过程中获得{dist}的min

library(tidyverse)
mget(ls(pattern = "_dat2$")) %>%
        imap(~ left_join(.x, zip, by = 'zip') %>%
             group_by(zip) %>%
             summarise((! str_c('dist_', substr(.y, 1, 1), '_min')  :=  min(dist)))

或者另一个选择是为重复的任务创建一个函数

joinSumm <- function(dat, groupName, colName, data2) {
    groupName <- enquo(groupName)
    colName <- enquo(colName)
    nm1 <- str_c('dist_', str_sub(rlang::as_name(enquo(dat)), 1, 1), '_min')
    dat %>%
       left_join(data2, by = rlang::as_name(groupName)) %>%
        group_by(!! groupName) %>%
        summarise((!! nm1) := min(!! colName))

  }
joinSumm(a_dat2, zip, dist, zip)
joinSumm(b_dat2, zip, dist, zip)

带有内置数据集iris(无连接部分)的可复制示例

list(a_dat = iris, b_dat = iris, c_dat = iris) %>% 
      imap(~ .x %>% 
            group_by(Species) %>%
            summarise(!! str_c('dist_', substr(.y, 1, 1), '_min') := min(Sepal.Length)))
#$a_dat
# A tibble: 3 x 2
#  Species    dist_a_min
#  <fct>           <dbl>
#1 setosa            4.3
#2 versicolor        4.9
#3 virginica         4.9

#$b_dat
# A tibble: 3 x 2
#  Species    dist_b_min
#  <fct>           <dbl>
#1 setosa            4.3
#2 versicolor        4.9
#3 virginica         4.9

$c_dat
# A tibble: 3 x 2
#  Species    dist_c_min
#  <fct>           <dbl>
#1 setosa            4.3
#2 versicolor        4.9
#3 virginica         4.9