gpt4 book ai didi

r - 子选择数据框

转载 作者:行者123 更新时间:2023-12-01 13:51:41 26 4
gpt4 key购买 nike

我想有个简单的问题。在我的数据框中,我想创建 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)

谢谢大家

丽珊娜

最佳答案

你甚至不需要 subset,检查:?"["

Quality_scoreComplete <- completefile[completefile$Quality_score %in% methods,]

已编辑:基于@Sacha Epskamp 的善意评论:表达式中的== 给出了错误的结果,因此将其更正为 %in%。谢谢!

问题示例:

> x <- c(17, 19)
> cars[cars$speed==x,]
speed dist
29 17 32
31 17 50
36 19 36
38 19 68
> cars[cars$speed %in% x,]
speed dist
29 17 32
30 17 40
31 17 50
36 19 36
37 19 46
38 19 68

关于r - 子选择数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5349758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com