I have the following dataset in R.
> jan_weather
X HighTemp WindSpeed Snowfall WindChill
1 1-Jan 19 8 0.0 9.0
2 2-Jan 23 11 0.2 12.0
3 3-Jan 25 9 0.0 15.7
4 4-Jan 33 14 7.6 23.2
5 5-Jan 38 7 0.0 32.7
6 6-Jan 34 6 2.1 28.6
7 7-Jan 35 12 0.0 26.6
8 8-Jan 27 15 0.0 15.2
9 9-Jan 28 11 3.3 18.2
10 10-Jan 30 17 1.4 18.3
11 11-Jan 36 13 0.0 27.4
12 12-Jan 44 9 0.0 39.0
13 13-Jan 41 8 0.0 35.8
14 14-Jan 34 14 0.0 24.5
15 15-Jan 31 15 0.0 20.3
16 16-Jan 32 12 2.8 22.8
17 17-Jan 35 18 0.0 24.5
18 18-Jan 20 22 0.0 3.6
19 19-Jan 17 24 0.0 -1.1
20 20-Jan 16 20 0.6 -1.0
21 21-Jan 19 8 0.0 9.0
22 22-Jan 25 9 0.0 15.7
23 23-Jan 28 13 14.9 17.3
24 24-Jan 23 11 0.0 12.0
25 25-Jan 28 32 0.0 11.7
26 26-Jan 33 13 0.0 23.6
27 27-Jan 31 15 3.7 20.3
28 28-Jan 39 10 0.0 32.4
29 29-Jan 36 4 0.0 32.7
30 30-Jan 33 8 0.9 26.1
31 31-Jan 37 10 0.0 29.9
I would like to find the dates where for which the wind chill value is no less than 22 degrees Fahrenheit, combined with for which the wind speed is also no greater than 13 miles per hour. I figure this is a simple if statement and storing the return in a variable, but I seem to be having problems on how to set it up. Any advice?
CodePudding user response:
We could use filter
from dplyr
:
library(dplyr)
df %>%
filter(WindChill > 22 & WindSpeed < 13)
or Base R:
subset(df, WindChill > 22 & WindSpeed < 13)
X HighTemp WindSpeed Snowfall WindChill
5 5-Jan 38 7 0.0 32.7
6 6-Jan 34 6 2.1 28.6
7 7-Jan 35 12 0.0 26.6
12 12-Jan 44 9 0.0 39.0
13 13-Jan 41 8 0.0 35.8
16 16-Jan 32 12 2.8 22.8
28 28-Jan 39 10 0.0 32.4
29 29-Jan 36 4 0.0 32.7
30 30-Jan 33 8 0.9 26.1
31 31-Jan 37 10 0.0 29.9
CodePudding user response:
base R
df[df$WindChill > 22 & df$WindSpeed < 13,]
Output
X HighTemp WindSpeed Snowfall WindChill
5 5-Jan 38 7 0.0 32.7
6 6-Jan 34 6 2.1 28.6
7 7-Jan 35 12 0.0 26.6
12 12-Jan 44 9 0.0 39.0
13 13-Jan 41 8 0.0 35.8
16 16-Jan 32 12 2.8 22.8
28 28-Jan 39 10 0.0 32.4
29 29-Jan 36 4 0.0 32.7
30 30-Jan 33 8 0.9 26.1
31 31-Jan 37 10 0.0 29.9
Or with data.table
library(data.table)
dt <- as.data.table(df)
dt[, .SD[WindChill > 22 & WindSpeed < 13]]
Data
df <- structure(list(X = c("1-Jan", "2-Jan", "3-Jan", "4-Jan", "5-Jan",
"6-Jan", "7-Jan", "8-Jan", "9-Jan", "10-Jan", "11-Jan", "12-Jan",
"13-Jan", "14-Jan", "15-Jan", "16-Jan", "17-Jan", "18-Jan", "19-Jan",
"20-Jan", "21-Jan", "22-Jan", "23-Jan", "24-Jan", "25-Jan", "26-Jan",
"27-Jan", "28-Jan", "29-Jan", "30-Jan", "31-Jan"), HighTemp = c(19L,
23L, 25L, 33L, 38L, 34L, 35L, 27L, 28L, 30L, 36L, 44L, 41L, 34L,
31L, 32L, 35L, 20L, 17L, 16L, 19L, 25L, 28L, 23L, 28L, 33L, 31L,
39L, 36L, 33L, 37L), WindSpeed = c(8L, 11L, 9L, 14L, 7L, 6L,
12L, 15L, 11L, 17L, 13L, 9L, 8L, 14L, 15L, 12L, 18L, 22L, 24L,
20L, 8L, 9L, 13L, 11L, 32L, 13L, 15L, 10L, 4L, 8L, 10L), Snowfall = c(0,
0.2, 0, 7.6, 0, 2.1, 0, 0, 3.3, 1.4, 0, 0, 0, 0, 0, 2.8, 0, 0,
0, 0.6, 0, 0, 14.9, 0, 0, 0, 3.7, 0, 0, 0.9, 0), WindChill = c(9,
12, 15.7, 23.2, 32.7, 28.6, 26.6, 15.2, 18.2, 18.3, 27.4, 39,
35.8, 24.5, 20.3, 22.8, 24.5, 3.6, -1.1, -1, 9, 15.7, 17.3, 12,
11.7, 23.6, 20.3, 32.4, 32.7, 26.1, 29.9)), class = "data.frame", row.names = c(NA,
-31L))