搜索R数据框中所有列的值 [英] Search values across all columns in R data frame
本文介绍了搜索R数据框中所有列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是一个示例数据框。
Here's a sample data frame.
df = data.frame(company = c('a', 'b', 'c', 'd'),
bond = c(0.2, 1, 0.3, 0),
equity = c(0.7, 0, 0.5, 1),
cash = c(0.1, 0, 0.2, 0))
df
company bond equity cash
1 a 0.2 0.7 0.1
2 b 1.0 0.0 0.0
3 c 0.3 0.5 0.2
4 d 0.0 1.0 0.0
我需要找到在任何列中都有1.0的公司。
预期结果应为 b和d 。
I need to find companies which have 1.0 in any columns. The expected result should be b and d.
请提供适用于> 20列的解决方案。
df%>%filter(bond == 1)
的解决方案仅适用于搜索特定列。
Please provide a solution that works for >20 columns.
Solutions like df %>% filter(bond == 1)
only works for searching a particular column.
dplyr
或 data.table
解决方案都可接受。
dplyr
or data.table
solutions are acceptable.
谢谢。
推荐答案
使用 rowSums
数据框应该工作:
df[rowSums(df[-1] == 1.0) != 0, 'company']
[1] b d
Levels: a b c d
这篇关于搜索R数据框中所有列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文