r - 如何在r中选择具有特定值的行?
问题描述
我正在尝试编辑我的数据框,但似乎找不到我需要解决这个问题的功能。
我有一个大致如下所示的数据框:
Title Description Rating
Beauty and the Beast a 2.5
Aladdin b 3
Coco c 2
等
(评级介于 1 和 3 之间)
我正在尝试编辑我的数据框,以便获得一个新的数据框,其中评级列没有十进制数字。
即:新的数据框将是:
Title Description Rating
Aladdin b 3
Coco c 2
因为 Beaty and the Beast 的评分不是 1、2 或 3。
我觉得 R 中有一个简单的函数,我在 Google 上找不到,我希望有人能提供帮助。
解决方案
我们可以使用subset
(from base R
) 来比较“Rating”的整数转换值
subset(df1, Rating == as.integer(Rating))
# Title Description Rating
#2 Aladdin b 3
#3 Coco c 2
或者,如果我们要与特定的一组值进行比较,请使用%in%
subset(df1, Rating %in% 1:3)
数据
df1 <- structure(list(Title = c("Beauty and the Beast", "Aladdin", "Coco"
), Description = c("a", "b", "c"), Rating = c(2.5, 3, 2)),
class = "data.frame", row.names = c(NA,
-3L))
推荐阅读
- flutter - 颤振和以太坊:未处理的异常:RangeError:值不在范围内:32
- elasticsearch - 如何在 Elasticsearch 中的字段上强制唯一性以避免重复
- python-3.x - CuDNNLSTM 在 TensorFlow2 中比 LSTM 慢
- xml - 使用 xsl:copy-of 时 xsl:namespace-alias 无法按预期工作
- iis - 每个请求的 Kerberos 挑战
- go - 如何使用 Go 语言从 bitbucket 上的远程存储库中获取文件
- html - 框架集不能在 EDGE 中运行,但运行良好 Mozilla/IE
- python - 通过make启动时,有什么方法可以查看shell脚本的输出,对于SAM
- java - 基于存储对象的属性修剪 flink 状态
- c++ - 避免隐式捕获这个?