简体   繁体   English

用其他东西代替 cur_data() 而不是 pick()

[英]Replace cur_data() with something else but not pick()

I get a warning (see below) for cur_data() but I am not sure how to use pick() instead.我收到cur_data()的警告(见下文),但我不确定如何改用pick() I don't use it to access columns but to refer to the current dataset in the pipe.我不使用它来访问列,而是引用 pipe 中的当前数据集。

You can use tidylog::filter() to see that 5 rows are dropped.您可以使用tidylog::filter()来查看删除了 5 行。

library(dplyr)

VARS <- c("mpg", "disp")

mtcars |> 
  filter(if_all(all_of(VARS), 
   ~ .x >= min( filter(cur_data(), as.logical(am)) |> pull(.x) ))) 
# Warning message:
#   There was 1 warning in `filter()`.
# ℹ In argument `..1 = ... & ...`.
# Caused by warning:
#   ! `cur_data()` was deprecated in dplyr 1.1.0.
# ℹ Please use `pick()` instead.

mtcars |> 
  filter(if_all(all_of(VARS), 
     ~ .x >= min( filter(.data, as.logical(am)) |> pull(.x) ))) 
# fails


sessionInfo()
# R version 4.2.0 (2022-04-22)
# Platform: aarch64-apple-darwin20 (64-bit)
# Running under: macOS 13.1
# 
# Matrix products: default
# LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] dplyr_1.0.99.9000

If it is a single column, then we don't need cur_data() , just convert am to logical to subset ( [ ) the column, get the min mum can check whether the column value is greater than or equal to the column value如果是单列,那我们就不需要cur_data()了,只需要将am转化为逻辑子集( [ )列,得到min mum就可以检查列值是否大于等于列值

library(dplyr)
out2 <- mtcars %>% 
    filter(if_all(all_of(VARS), ~ .x >= min(.x[as.logical(am)] )))

-checking with OP's output - 检查 OP 的 output

> out1 <- mtcars |> 
+   filter(if_all(all_of(VARS), 
+    ~ .x >= min( filter(cur_data(), as.logical(am)) |> pull(.x) ))) 
> all.equal(out1, out2)
[1] TRUE

If we need to use pick (from the devel version of dplyr )如果我们需要使用pick (来自dplyr的开发版本)

out3 <- mtcars |> 
  filter(if_all(all_of(VARS), 
   ~ .x >= min( filter(pick(everything()), 
     as.logical(am)) |> pull(.x) )))

-checking -检查

> all.equal(out2, out3)
[1] TRUE

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

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