简体   繁体   English

R 通过列嵌套 map

[英]R nested map through columns

I got a function which was solved here.我有一个 function 在这里解决了。
This function takes a column filled with annotations and another grouping column and propagates the annotation to rows with missing values.此 function 采用一个填充了注释的列和另一个分组列,并将注释传播到具有缺失值的行。

f1 <- function(data, group_col, expand_col){
  data %>%
    dplyr::group_by({{group_col}}) %>%
    dplyr::mutate( 
      {{expand_col}} := dplyr::case_when(
        !is.na({{expand_col}}) ~ 
          {{expand_col}} ,
      any( !is.na({{expand_col}})  ) & is.na({{expand_col}}) ~ 
        paste(unique(unlist(str_split(na.omit({{expand_col}}), " ")) ), 
                         collapse = " "),
      TRUE ~ 
        NA_character_  
    ))  %>%
    dplyr::ungroup()
}  

Now I would like to do it through many columns grouping columns ( group_col ) and annotations columns ( expand_col ).现在我想通过许多列分组列( group_col )和注释列( expand_col )来做到这一点。
So if I have this df:所以如果我有这个df:

t <- tibble(a = c("a", "b", "c", "d", "e", "f", "g", "h"), 
            b = c(  1,   1,   1,   1,   2,   2,   2,   2),
            c = c(  1,   1,   2,   2,   3,   3,   4,   4),
            d = c( NA,  NA,  NA, "D", "E",  NA,  NA,  NA),
            e = c("A",  NA, "C",  NA,  NA,  NA, "G", "H")
            )

I may apply it like this我可以这样应用它

> t %>%
+   f1(c,e) %>%
+   f1(b,e) %>%
+   f1(c,d) %>%
+   f1(b,d)
# A tibble: 8 x 5
  a         b     c d     e    
  <chr> <dbl> <dbl> <chr> <chr>
1 a         1     1 D     A    
2 b         1     1 D     A    
3 c         1     2 D     C    
4 d         1     2 D     C    
5 e         2     3 E     G H  
6 f         2     3 E     G H  
7 g         2     4 E     G    
8 h         2     4 E     H    

So, I have 3 groups of columns, the ids, the grouping columns (2:3), and annotation columns (4:5).所以,我有 3 组列,ID、分组列 (2:3) 和注释列 (4:5)。
Since I call the function many times, I'd like to know how to use the map function to pass the column indexes to apply the function like in the example above. Since I call the function many times, I'd like to know how to use the map function to pass the column indexes to apply the function like in the example above.

I tried something like this我尝试过这样的事情

3:2 %>% 
  map(
    function(x) 4:5 %>% 
      map(
        function(y) f1(
          t, 
          !!(colnames(t)[x]) , 
          !!(colnames(t)[y])
        ) 
      )
  )

But the result is a wrong mess.但结果是一团糟。

Thanks in advance提前致谢

This can be done easily in a for loop这可以在for循环中轻松完成

i1 <- rep(names(t)[3:2], 2)
i2 <- rep(names(t)[4:5], each = 2)
for(i in seq_along(i1))
t <- f1(t, !! rlang::sym(i1[i]), !! rlang::sym(i2[i]))
t
# A tibble: 8 x 5
#  a         b     c d     e    
#  <chr> <dbl> <dbl> <chr> <chr>
#1 a         1     1 D     A    
#2 b         1     1 D     A    
#3 c         1     2 D     C    
#4 d         1     2 D     C    
#5 e         2     3 E     G H  
#6 f         2     3 E     G H  
#7 g         2     4 E     G    
#8 h         2     4 E     H    

Since f1 accepts column names, you need to first convert your indices to symbols:由于f1接受列名,因此您需要首先将索引转换为符号:

v1 <- rlang::syms( colnames(t)[3:2] )
v2 <- rlang::syms( colnames(t)[4:5] )

Now, you can use tidyr::crossing() to get all possible pairs of your symbols, and purrr::reduce2() to sequentially apply f1() with those symbols:现在,您可以使用tidyr::crossing()来获取所有可能的符号对,并purrr::reduce2()依次将f1()应用于这些符号:

V <- tidyr::crossing( v1, v2 )
Res <- purrr::reduce2( V$v1, V$v2, f1, .init=t )

# Validation
Res2 <- t %>% f1(c,e) %>% f1(b,e) %>% f1(c,d) %>% f1(b,d)
identical(Res, Res2)   # TRUE

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

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