[英]How to select rows with a certain value in r?
I am trying to edit my dataframe but cannot seem to find the function that I need to sort this out.我正在尝试编辑我的 dataframe 但似乎找不到我需要整理出来的 function。
I have a dataframe that looks roughly like this:我有一个 dataframe 大致如下所示:
Title Description Rating
Beauty and the Beast a 2.5
Aladdin b 3
Coco c 2
etc.等等
(rating is between 1 and 3) (评分介于 1 和 3 之间)
I am trying to edit my dataframe so that I get a new dataframe where there is no decimal numbers for the rating column.我正在尝试编辑我的 dataframe 以便获得一个新的 dataframe ,其中评级列没有十进制数字。
ie: the new dataframe would be:即:新的 dataframe 将是:
Title Description Rating
Aladdin b 3
Coco c 2
As Beaty and the Beast's rating is not 1, 2 or 3.因为 Beaty and the Beast 的评分不是 1、2 或 3。
I feel like there's a simple function in R that I just cannot find on Google, and I was hoping someone could help.我觉得 R 中有一个简单的 function ,我在 Google 上找不到,我希望有人能提供帮助。
We can use subset
(from base R
) with a comparison on the integer converted values of 'Rating'我们可以使用subset
(来自base R
)与 integer 转换后的“评级”值进行比较
subset(df1, Rating == as.integer(Rating))
# Title Description Rating
#2 Aladdin b 3
#3 Coco c 2
Or if we are comparing with specific set of values, use %in%
或者,如果我们要与特定的一组值进行比较,请使用%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))
You can get the remainder after dividing by 1 and select rows where the remainder is 0.你可以得到除以 1 和 select 行后余数为 0 的余数。
subset(df, Rating %% 1 == 0)
# Title Description Rating
#2 Aladdin b 3
#3 Coco c 2
You want to use the dplyr function in R您想在 R 中使用 dplyr function
library(dplyr)
df1 %>%
filter(R != 2.5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.