根据变量进行过滤,以允许返回所有行。

huangapple go评论65阅读模式
英文:

Filtering by variable to allow returning all rows

问题

我有以下类似的代码:

library(dplyr)
my_values <- rep(1:10, 10)
my_df <- data.frame(my_values, "points")

user_values <- c(2, 3)

filter(my_df, my_values %in% user_values)

用户提供了要进行筛选的特定值。在某些情况下,筛选应该不处于“活动”状态,这意味着应保留具有所有“my_values”值的行。

我可以分配什么给user_values以实现这一点? 我尝试分配NULL,但返回0行。分配TRUE将其强制转换为1的值。

我知道我可以分配my_df$my_values(或unique(my_df$my_values)),但在变量可能取数千个可能值的情况下,通过该方式进行筛选可能需要很长时间。因此,我正在寻找更优雅的解决方案。

英文:

I have code like the following:

library(dplyr)
my_values &lt;- rep(1:10, 10)
my_df &lt;- data.frame(my_values, &quot;points&quot;)

user_values &lt;- c(2, 3)

filter(my_df, my_values %in% user_values)

The user provides certain values to filter by. In certain situations, the filter should not be "active", meaning rows with all values of "my_values" should be retained.

What can I assign to user_values to achieve this? I tried assigning NULL, but that returns 0 rows. Assigning TRUE coerces it to the value of 1.

I know I could assign my_df$my_values (or unique(my_df$my_values)), but filtering by that would probably take a long time when there are thousands of possible values for the variable to take. So I'm searching for a more elegant solution.

答案1

得分: 3

你可以在 filter 函数调用中使用 if

filter(my_df, if(is.null(user_values)) TRUE else my_values %in% user_values)
英文:

You can use if inside the filter function call:

filter(my_df, if(is.null(user_values)) TRUE else my_values %in% user_values)

答案2

得分: 3

我会使用TRUE,因为从语义上来说,它可以被理解为"everything"。正如@Konrad所指出的,NULL也是一个选项,最终取决于项目中的个人喜好和约定:

nrow(my_df)
# [1] 100

user_values <- c(2, 3)
my_df %>%
   filter(isTRUE(user_values) | my_values %in% user_values) %>%
   nrow()
# [1] 20

user_values <- TRUE
my_df %>%
   filter(isTRUE(user_values) | my_values %in% user_values) %>%
   nrow()
# [1] 100

user_values <- NULL
my_df %>%
   filter(is.null(user_values) | my_values %in% user_values) %>%
   nrow()
# [1] 100
英文:

I would use TRUE as it could be understood as "everything" semantically. NULL would be also an option as pointed out by @Konrad and eventually it breaks down to a matter of taste / overall conventions in your project:

nrow(my_df)
# [1] 100

user_values &lt;- c(2, 3)
my_df %&gt;% 
   filter(isTRUE(user_values) | my_values %in% user_values) %&gt;%
   nrow()
# [1] 20

user_values &lt;- TRUE
my_df %&gt;% 
   filter(isTRUE(user_values) | my_values %in% user_values) %&gt;%
   nrow()
# [1] 100

user_values &lt;- NULL
my_df %&gt;% 
   filter(is.null(user_values) | my_values %in% user_values) %&gt;%
   nrow()
# [1] 100

答案3

得分: 0

一个可能性是测试 `user_values` 的 `length`。如果长度为 `0`,则返回所有内容,否则使用 `filter`。
如果(length(user_values) > 0) filter(my_df, my_values %in% user_values) else my_df
另一种方法是将其嵌套在 `filter` 中,如 @thothal 所示。
filter(my_df, length(user_values) == 0 | my_values %in% user_values)
或者像 @Konrad Rudolph 所示,在 `filter` 中放置 `if` 条件。
filter(my_df, if(length(user_values) == 0) TRUE else my_values %in% user_values)
以下的 `user_values` 设置将返回 `my_df` 的所有行。
user_values <- c()
user_values <- logical()
user_values <- vector()
user_values <- character()
user_values <- numeric()
user_values <- NULL
英文:

A possibility would be to test the length of user_values. If length is 0 then return all otherwise use filter.

if(length(user_values) &gt; 0) filter(my_df, my_values %in% user_values) else my_df

Alternatively this could also be used inside filter like shown by @thothal.

filter(my_df, length(user_values) == 0 | my_values %in% user_values)

Or place the if condition inside filter as shown by @Konrad Rudolph.

filter(my_df, if(length(user_values) == 0) TRUE else my_values %in% user_values)

The following settings of user_values will give here all rows of my_df.

user_values &lt;- c()
user_values &lt;- logical()
user_values &lt;- vector()
user_values &lt;- character()
user_values &lt;- numeric()
user_values &lt;- NULL

huangapple
  • 本文由 发表于 2023年6月19日 15:01:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定