'$'运算符在这个R代码片段中做了什么?

Dir*_*irk 2 r dplyr

考虑以下R dplyr代码:

lahmanNames %>% 
  bind_rows(.id = "dataframe") %>%   # Bind the data frames
  filter(var == "playerID") %>%   # Filter the results
  `$`(dataframe)   #  <---- WHAT DOES $ MEAN?
Run Code Online (Sandbox Code Playgroud)

在这种情况下$运算符引用了什么?

aus*_*sen 7

这就像做mtcars$dataframe.有很多方法可以做到这一点.在dplyr0.7.0 pull中添加了函数,它以与piping(%>%)一起使用的方式实现.


library(dplyr)

mtcars %>% `$`(mpg)

#>  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2
#> [15] 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4
#> [29] 15.8 19.7 15.0 21.4

mtcars$mpg

#>  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2
#> [15] 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4
#> [29] 15.8 19.7 15.0 21.4

mtcars[["mpg"]]

#>  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2
#> [15] 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4
#> [29] 15.8 19.7 15.0 21.4

mtcars %>% pull(mpg)

#>  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2
#> [15] 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4
#> [29] 15.8 19.7 15.0 21.4
Run Code Online (Sandbox Code Playgroud)