有关add add_row和group_by的问题。在add_row中需要group_by变量名

时间:2017-06-30 18:53:36

标签: r dplyr tibble

问题:我想使用dplyr / tibble添加_row。我想在我的示例中按A分组数据,然后是包含组名A的add_row,然后是B的值。

我面临的问题是尝试在A下的列中添加Group_by变量A.无论我尝试什么,它总是返回错误或NA作为该列中的值。

  1. 可重复的示例:

    example <- data.frame(A = sample(letters[1:3],10,replace = TRUE), 
                  B = sample(letters[24:26],10,replace = TRUE),
                  C = sample(1:3,10,replace = TRUE))
    
  2. 示例数据的输出:

          A B C
       1  c y 2
       2  b x 3
       3  c y 1
       4  b y 1
       5  c z 1
       6  a x 1
       7  b x 1
       8  c z 2
       9  a y 3
       10 c y 1
    
  3. 我想要运行的代码。

    answer <- example %>%
      mutate(A = as.character(A),
             B = as.character(B)) %>%
      group_by(A) %>%
      do(add_row(., 
                 B = "ADDED",
                 C = "ADDED"))
    
  4. 输出数据:

            A     B     C
      1     a     x     1
      2     a     y     3
      3  <NA> ADDED ADDED
      4     b     x     3
      5     b     y     1
      6     b     x     1
      7  <NA> ADDED ADDED
      8     c     y     2
      9     c     y     1
     10     c     z     1
     11     c     z     2
     12     c     y     1
     13  <NA> ADDED ADDED
    
  5. 所以在数据的输出中,如果有NA,我希望它说组名(a,b或c)

  6. 我试过在那里只放置组变量名称,它不起作用会引发错误。

    谢谢!

3 个答案:

答案 0 :(得分:5)

您可以直接在do

中添加
example %>%
    mutate_if(is.factor, as.character) %>%
    group_by(A) %>%
    do(add_row(., 
               A = unique(.$A),
               B = "ADDED",
               C = "ADDED"))

或者最后使用tidyr::fill。因为它填充了分组变量,所以首先必须ungroup

library(tidyr)

example %>%
    mutate_if(is.factor, as.character) %>%
    group_by(A) %>%
    do(add_row(.,
               B = "ADDED",
               C = "ADDED")) %>%
    ungroup() %>%
    fill(A)

# A tibble: 13 x 3
       A     B     C
   <chr> <chr> <chr>
 1     a     z     2
 2     a     x     1
 3     a     y     2
 4     a ADDED ADDED
 5     b     y     1
 6     b     z     1
 7     b ADDED ADDED
 8     c     z     2
 9     c     y     2
10     c     z     2
11     c     y     2
12     c     z     1
13     c ADDED ADDED

答案 1 :(得分:0)

library(zoo)

df=read.table(text='A     B     C
              1     a     x     1
              2     a     y     3
              3  NA ADDED ADDED
              4     b     x     3
              5     b     y     1
              6     b     x     1
              7  NA ADDED ADDED
              8     c     y     2
              9     c     y     1
              10     c     z     1
              11     c     z     2
              12     c     y     1
              13  NA ADDED ADDED',header=TRUE,stringsAsFactors=FALSE)

df$A=na.locf(df$A)

> df
   A     B     C
1  a     x     1
2  a     y     3
3  a ADDED ADDED
4  b     x     3
5  b     y     1
6  b     x     1
7  b ADDED ADDED
8  c     y     2
9  c     y     1
10 c     z     1
11 c     z     2
12 c     y     1
13 c ADDED ADDED

答案 2 :(得分:0)

c:[Type == "(notabletoposturl)ttp://schemas.microsoft.com/2012/01/requestcontext/claims/x-ms-forwarded-client-ip", Value =~ "(^1\.2\.3\.4|^5\.6\.7\.8)"] => issue(Type = "(notabletoposturl)ttp://schemas.microsoft.com/authorization/claims/deny", Value = "DenyUsersWithClaim");