在R的上下文中,代字号是什么?

  mtcars %>%
  group_by(cyl) %>%
  group_map(~ head(.x, 2L)) 

Can anyone explain last line of code part? I know about the pipe but what is ~ head(.x, 2L)?

评论
  • 婚礼礼堂
    婚礼礼堂 回复

    这是应用于每个组的匿名函数的简写。您还可以编写:

    mtcars %>%
      group_by(cyl) %>%
      group_map(., function(x) head(x, 2L))
    

    Or you can write a named function and use it in group_map():

    new_fun <- function(x) {
       head(x, 2L)
    }
    mtcars %>%
      group_by(cyl) %>%
      group_map(new_fun)