是否可以根据列中的任何值选择R中的列?

发布于 2025-02-09 20:41:43 字数 416 浏览 4 评论 0原文

我想将我的DF子集以包括在任何行中包含一定值的列。

例如,如果我有:

year = c(1990,1991,1992,1993,1994,1995,1996,1997,1998,1999)
apple = c(1,4,6,8,9,9,2,4,7,4)
orange = c(7,1,5,5,2,1,7,1,3,8)
banana = c(9,9,4,8,1,3,6,7,5,9)
lemon = c(8,3,3,3,2,5,6,7,2,4)
df = data.frame(year,apple,orange,banana,lemon)

df

我只想选择该列中任何位置的列,以便我的DF仅包括Apple和Banana列。

这可能吗?到目前为止,我发现的所有答案仅启用基于列名的选择列,但是我想根据列中的单元格值选择。谢谢你!

I want to subset my df to include only columns that include a certain value in any row.

for example, if I have:

year = c(1990,1991,1992,1993,1994,1995,1996,1997,1998,1999)
apple = c(1,4,6,8,9,9,2,4,7,4)
orange = c(7,1,5,5,2,1,7,1,3,8)
banana = c(9,9,4,8,1,3,6,7,5,9)
lemon = c(8,3,3,3,2,5,6,7,2,4)
df = data.frame(year,apple,orange,banana,lemon)

df

I want to select only the columns that have a 9 anywhere in the column so that my df would become only include the apple and banana columns.

Is this possible? All the answers I've found so far only enable selecting columns based on the column name, but I want to select based on cell values within the column. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心是晴朗的。 2025-02-16 20:41:43

我们可以在中传递中的中的函数,其中 - 检查列是否为数字,如果是数字,请检查是否有任何值等于到9。此外,可以将将任何(.x == 9)更改为9%in%.x

library(dplyr)
df %>% 
  select(where(~is.numeric(.x) && any(.x == 9)))

-输出

 apple banana
1      1      9
2      4      9
3      6      4
4      8      8
5      9      1
6      9      3
7      2      6
8      4      7
9      7      5
10     4      9

We can pass a function in select within where - check whether the column is numeric and if that is numeric, check whether there are any value equal to 9. In addition can change the any(.x ==9) to 9 %in% .x.

library(dplyr)
df %>% 
  select(where(~is.numeric(.x) && any(.x == 9)))

-output

 apple banana
1      1      9
2      4      9
3      6      4
4      8      8
5      9      1
6      9      3
7      2      6
8      4      7
9      7      5
10     4      9
愚人国度 2025-02-16 20:41:43

基本r使用过滤器:输出:

Filter(function(x) any(x == 9), df)

输出:

   apple banana
1      1      9
2      4      9
3      6      4
4      8      8
5      9      1
6      9      3
7      2      6
8      4      7
9      7      5
10     4      9

base R option using Filter:

Filter(function(x) any(x == 9), df)

Output:

   apple banana
1      1      9
2      4      9
3      6      4
4      8      8
5      9      1
6      9      3
7      2      6
8      4      7
9      7      5
10     4      9
甜味超标? 2025-02-16 20:41:43

这是漫长的,太冗长了,@akrun已经提供了完美的答案。
因此,这里是另一种方法:
我们在这里做的是突变 遍历除检查它们是否包含9如果是这样。列的名称为新列,称为x

然后使用Any_of 选择

library(dplyr)
library(tidyr)

df %>% 
  mutate(across(-year, ~case_when(. == 9 ~ cur_column()), .names = 'new_{col}')) %>%
  unite(x, starts_with('new'), na.rm = TRUE, sep = ' ') %>% 
  select(any_of(x))


   banana apple
1       9     1
2       9     4
3       4     6
4       8     8
5       1     9
6       3     9
7       6     2
8       7     4
9       5     7
10      9     4

This one is long and too verbose and the perfect answer is already provided by @akrun.
Therefore here an alternative approach:
What we do here is mutate across each column except year check if they contain a 9 if so then put the name of column to a new column called x,

then use any_of with select

library(dplyr)
library(tidyr)

df %>% 
  mutate(across(-year, ~case_when(. == 9 ~ cur_column()), .names = 'new_{col}')) %>%
  unite(x, starts_with('new'), na.rm = TRUE, sep = ' ') %>% 
  select(any_of(x))


   banana apple
1       9     1
2       9     4
3       4     6
4       8     8
5       1     9
6       3     9
7       6     2
8       7     4
9       5     7
10      9     4

大概我们可以使用colmeans类似于下面的,如果在列中至少存在一个9,则平均值应非零

> df[colMeans(df == 9) > 0]
   apple banana
1      1      9
2      4      9
3      6      4
4      8      8
5      9      1
6      9      3
7      2      6
8      4      7
9      7      5
10     4      9

Probably we can use colMeans like below, where the mean value should be non-zero if there exists at least one 9 in the column

> df[colMeans(df == 9) > 0]
   apple banana
1      1      9
2      4      9
3      6      4
4      8      8
5      9      1
6      9      3
7      2      6
8      4      7
9      7      5
10     4      9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文