'R: Elegant Way to Filter Dataframe Such that First Few Decimals of a Set of Variables Are Considered
I have this toy data which I want to get only rows that have 0.5 in the v2 variable and also have 0.3 in the v3 variable.
I have tried this:
library(tidyverse)
toy_data <- tibble(v1 = c(20215, 20549, 21678, 20562, 20245, 20225, 21245, 22322, 20618, 21993, 22394, 21581), v2 = c(0.612, 0.618, 0.642, 0.618, 0.612, 0.593, 0.659, 0.619, 0.651, 0.662, 0.640, 0.509), v3 = c(0.533, 0.567, 0.469, 0.545, 0.675, 0.399, 0.322, 0.543, 0.576, 0.457, 0.552, 0.390), v4 = c(49, 118, 257, 384, 566, 569, 637, 1028, 1253, 2277, 2300, 2390), v5 = rep(NA, 12))
toy_data |> filter(v2 >= 0.5, v3 >= 0.3) |> filter(v2 < 0.6, v3 < 0.4)
## A tibble: 2 x 5
# v1 v2 v3 v4 v5
# <dbl> <dbl> <dbl> <dbl> <lgl>
#1 20225 0.593 0.399 569 NA
#2 21581 0.509 0.39 2390 NA
.
What I Want
Is there an elegant way to do this not necessarily by tidyversr::filter() such that I can tell R to look for 0.5 in variable v2 and 0.3 in variable v3 like this:
toy_data |> substr(toy_data$v2, 1, 3) %in% 0.5 & substr(toy_data$v3, 1, 3) %in% 0.3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
