'Filter data for whole numbers (.00)
I'm looking to extract the value of my data frame that are whole numbers, i.e. end in .00
Price
100.00
100.46
101.00
101.67
The resulting data frame would be:
Price
100.00
101.00
Solution 1:[1]
Maybe this could help.
%%
operator returns the remainder of a division, so 100 %% 1
equals zero and with a !
mark we can negate it. !
is Logical Not. In this case it turns zero (FALSE
) to TRUE
. Then filter
returns all those rows where corresponding values of the operation !(Price %% 1)
equals to TRUE
.
library(dplyr)
df %>%
filter(!(Price %% 1))
Price
1 100
2 101
Data
structure(list(Price = c(100, 100.46, 101, 101.67)), class = "data.frame", row.names = c(NA,
-4L))
Also for more use-cases on dplyr::filter
check the documentation.
Solution 2:[2]
Another option is to convert to integer
and do a ==
subset(df, Price == as.integer(Price))
Solution 3:[3]
Not that efficient but it can also work:
dplyr::filter(df, !stringr::str_detect(Price, "\\."))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | akrun |
Solution 3 | jpdugo17 |