代码之家  ›  专栏  ›  技术社区  ›  בנימן הגלילי

带magrittr的条件赋值帧$col<-val

  •  2
  • בנימן הגלילי  · 技术社区  · 9 年前

    基于Iris$Petal分配Iris$column的Magrittr语法是什么。长没有Magrittr的示例:

    df      <- iris[47:56,]
    df$val1 <- NA                                           ## create column
    df$val1[which(df$Petal.Length < 1.52)]                         <- "cake"
    df$val1[which(df$Petal.Length > 1.55 & df$Petal.Length <=4.55)] <- "pie"
    df$val1[which(df$Petal.Length > 4.55)]                        <- "apple"
    
    head(df)
    

    Petal.Length Petal.Width  Species    val1
    
    1.6                0.2     setosa     pie
    
    1.4                0.2     setosa     cake
    
    1.5                0.2     setosa     cake
    
    1.4                0.2     setosa     cake
    
    1.4                1.4.  versicolor   apple
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   moodymudskipper    8 年前

    magrittr 您编写的语法是:

    df %>% transform(val1 = NA) %$%
      inset(.,Petal.Length < 1.52,"val1","cake") %$%
      inset(.,Petal.Length > 1.55 & Petal.Length <= 4.55,"val1","pie") %$%
      inset(.,Petal.Length > 4.55,"val1","apple")
    

    马格里特 的别名:

    df %>% transform(val1 = NA) %$%
      inset(.,Petal.Length %>% is_less_than(1.52),"val1","cake") %$%
      inset(.,Petal.Length %>% is_greater_than(1.55) & Petal.Length %>% 
      is_weakly_less_than(4.55),"val1","pie") %$%
      inset(.,Petal.Length %>% is_greater_than(4.55),"val1","apple")
    

    df %>% transform(val1 = NA) %$%
      inset(.,Petal.Length %>% is_less_than(1.52),"val1","cake") %$%
      inset(.,Petal.Length %>% {is_greater_than(.,1.55) & is_weakly_less_than(.,4.55)},"val1","pie") %$%
      inset(.,Petal.Length %>% is_greater_than(4.55),"val1","apple")
    

    前两个在基座中严格等效(管道除外!):

    df %>% transform(val1 = NA) %$%
      `[<-`(.,Petal.Length < 1.52,"val1","cake") %$%
      `[<-`(.,Petal.Length > 1.55 & Petal.Length <= 4.55,"val1","pie") %$%
      `[<-`(.,Petal.Length > 4.55,"val1","apple")
    

    该变体相当于:

    df %>% transform(val1 = NA) %$%
      `[<-`(.,Petal.Length < 1.52,"val1","cake") %$%
      `[<-`(.,Petal.Length %>% {`>`(.,1.55) & `<=`(.,4.55)},"val1","pie") %$%
      `[<-`(.,Petal.Length > 4.55,"val1","apple")
    

    我用过 transform 因为这是一个 base mutate dplyr 功能,但它们在这里的工作方式相同。

    有关所有别名的定义,请参阅: ?extract

        2
  •  1
  •   akrun    9 年前

    case_when

    res <- df %>% 
              mutate(val1 = case_when(Petal.Length < 1.52 ~ 'cake',
                      Petal.Length > 1.55 & Petal.Length <= 4.55 ~ 'pie',
                       Petal.Length > 4.55 ~'apple'))
    head(res, 5)
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species  val1
    #1          5.1         3.8          1.6         0.2     setosa   pie
    #2          4.6         3.2          1.4         0.2     setosa  cake
    #3          5.3         3.7          1.5         0.2     setosa  cake
    #4          5.0         3.3          1.4         0.2     setosa  cake
    #5          7.0         3.2          4.7         1.4 versicolor apple
    
    推荐文章