I've got data.frame like below
ID country age
1 X 83
2 X 15
3 Y 2
4 Y 12
5 X 2
6 Y 2
7 Y 18
8 X 85
I need to filter rows for age below 10 and at the same time above 80.
How can I do it in the simplest way? For one condition I can use filter(data.frame, age > 80)
but I don't know how to do it for two conditions at the same time?
Type a comma, and then type the condition for the filter, such as C3:C50>3 (To set a condition, first type the address of the "criteria column" such as B1:B, then type an operator symbol such as greater than (>), and then type the criteria, such as the number 3.
I'm not sure from the question if you want the values between 10 and 80 or those below ten and above 80. If you want those between, you can put multiple arguments in filter
. If you want those below 10 and above 80 you can use |
as an "or" operator:
library(tidyverse)
data %>%
filter(age > 10,
age < 80)
data %>%
filter(age < 10 | age > 80)
Following may help you here too using dplyr
library(dplyr)
##Creating variable dat here which has values in it.
dat <- read.table(text = "ID country age
1 X 83
2 X 15
3 Y 2
4 Y 12
5 X 2
6 Y 2
7 Y 18
8 X 85",
header = TRUE)
dat %>%
filter(age<10 | age>80)
dat <- read.table(text = "ID country age
1 X 83
2 X 15
3 Y 2
4 Y 12
5 X 2
6 Y 2
7 Y 18
8 X 85",
header = TRUE)
x<-filter(dat, dat$age>80&dat$age<10)
x
Now, you can either use the OR (|) condition or you can use the AND (&) condition based on what you're actually trying to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With