子选择数据框
我想我有一个简单的问题。在我的数据框中,我想创建子集,其中列 Quality_score 等于:Perfect、Perfect*、Perfect*、Good、Good**好***
这在我的解决方案中:
>Quality_scoreComplete <- subset(completefile,Quality_score == "Perfect" | Quality_score=="Perfect***" | Quality_score=="Perfect****" | Quality_score=="Good" | Quality_score=="Good***" | Quality_score=="Good****")
有没有办法简化这个方法?喜欢:
methods<-c('Perfect', 'Perfect***', 'Perfect****', 'Good', 'Good***','Good***')
Quality_scoreComplete <- subset(completefile,Quality_score==methods)
谢谢大家,
莉莎娜
I have a simple questioon I think. In my dataframe I would like to make subset where column Quality_score is equal to: Perfect, Perfect*, Perfect*, Good, Good** and Good***
This in my solution by now:
>Quality_scoreComplete <- subset(completefile,Quality_score == "Perfect" | Quality_score=="Perfect***" | Quality_score=="Perfect****" | Quality_score=="Good" | Quality_score=="Good***" | Quality_score=="Good****")
Is there a way to simplify this method? Like:
methods<-c('Perfect', 'Perfect***', 'Perfect****', 'Good', 'Good***','Good***')
Quality_scoreComplete <- subset(completefile,Quality_score==methods)
Thank you all,
Lisanne
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你甚至不需要
子集
,检查:?"["
编辑:基于@Sacha Epskamp的善意评论:
==表达式中的
给出了错误的结果,因此将其更正为%in%
。谢谢!问题示例:
You do not even need
subset
, check:?"["
EDITED: based on kind comment of @Sacha Epskamp:
==
in the expression gives wrong results, so corrected it above to%in%
. Thanks!Example of the problem:
一个有效的方法是
grepl
,它在字符串中搜索模式并返回一个逻辑指示它是否存在。您还可以在字符串中使用|
运算符来指示 OR,并使用ignore.case
来忽略区分大小写:编辑:我现在发现区分大小写不是问题,谢谢阅读障碍!你可以简化为:
One thing that works is
grepl
, this searches for a pattern in strings and returns a logical indicating if it is there. You can use the|
operator in a string as well to indicate OR, andignore.case
to ignore case sensitivity:EDIT: I see now that case sensitivity was not an issue, thanks dyslexia! You could simplify then to: