What would be a more efficient way to create the data frames for each type of storm?

Viewed 58

Desired output: a df for each of the types storms based on the highest level of storm for each storm name. I could only figure out how to do this using status and it seemed to take me many steps by excluding data instead of obtaining the desired output directly.

  1. How could the code be written in a more efficient way using status?
  2. How could the code be written using category instead?

Data: I am working with dplyr and the data package 'storms'. https://dplyr.tidyverse.org/reference/storms.html

Sample data (status of storm over time, so many storms have more than one status/category):

Name Status Category
Barry tropical depression -1
Barry tropical storm 0
Barry hurricane 1 to 5

Here is how I obtained the desired output (I had already created a df of storms from only 2019-2021):

# find names of only hurricanes
name_of_hurricanes.df <- storms_19_20_21.df %>%
  filter(status=="hurricane") %>%
  distinct(name)


# hurricanes df

hurricane_data.df <- semi_join(storms_19_20_21.df, name_of_hurricanes.df, by = 'name', na_matches = "na") 
 

# find names of tropical storms and hurricanes, compare to hurricanes list and ouput those names that are not in the hurricanes df

name_of_ts_hurricanes.df <- storms_19_20_21.df %>%
  filter(status=="tropical storm") %>%
  distinct(name)

name_of_tropical_storms.df <- anti_join(name_of_ts_hurricanes.df, name_of_hurricanes.df, by = 'name', na_matches = "na")


# tropical storms df

tropical_storm_data.df <- semi_join(storms_19_20_21.df, name_of_tropical_storms.df, by = 'name', na_matches = "na") 


# tropical depressions df by excluding names of hurricanes and names of tropical storms

tropical_depression_data.df <- storms_19_20_21.df %>%
  anti_join(name_of_hurricanes.df, by = 'name', na_matches = "na") %>%
  anti_join(name_of_tropical_storms.df, by = 'name', na_matches = "na")
1 Answers

Here is one option with tidyverse. For each storm, we can get status name for the highest category. Then, we can use that column to split the data into the list of dataframes (as shown in the output). Here, I use split as it will name each dataframe, then I use the map statement to remove the highest column in each dataframe.

library(tidyverse)

result <- df %>% 
  group_by(name) %>% 
  mutate(highest = status[which.max(category)]) %>% 
  split(., .$highest) %>% 
  map(., ~ select(.x, -highest))

If you want to save each type of storm into its own dataframe, then we can use list2env, which will save each individual dataframe into the global environment.

list2env(result, envir = .GlobalEnv)

Another option is to use tidytable, which allows you to name the group and remove the grouping column:

library(tidytable)

result <- storms_19_20_21.df %>% 
  group_by(name) %>% 
  mutate(highest = status[which.max(category)]) %>% 
  group_by(highest) %>% 
  group_split.(highest, .keep = FALSE, .named = TRUE)

Output

$hurricane
# A tibble: 521 × 13
# Groups:   name [18]
   name   year month   day  hour   lat  long status              category  wind pressure tropicalstorm_force_diameter hurricane_force_diameter
   <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr>               <ord>    <int>    <int>                        <int>                    <int>
 1 Barry  2019     7    11     0  27.8 -87.6 tropical depression -1          30     1008                            0                        0
 2 Barry  2019     7    11     6  27.7 -88   tropical storm      0           35     1007                          100                        0
 3 Barry  2019     7    11    12  27.7 -88.4 tropical storm      0           35     1005                          100                        0
 4 Barry  2019     7    11    18  27.7 -88.8 tropical storm      0           40     1005                          170                        0
 5 Barry  2019     7    12     0  27.8 -89.2 tropical storm      0           45     1001                          170                        0
 6 Barry  2019     7    12     6  27.9 -89.7 tropical storm      0           45     1001                          180                        0
 7 Barry  2019     7    12    12  28.1 -90.2 tropical storm      0           50      998                          190                        0
 8 Barry  2019     7    12    18  28.4 -90.7 tropical storm      0           55      993                          220                        0
 9 Barry  2019     7    13     0  28.6 -91.1 tropical storm      0           60      993                          220                        0
10 Barry  2019     7    13     6  28.9 -91.5 tropical storm      0           60      993                          220                        0
# … with 511 more rows

$`tropical depression`
# A tibble: 17 × 13
# Groups:   name [3]
   name     year month   day  hour   lat  long status              category  wind pressure tropicalstorm_force_diameter hurricane_force_diameter
   <chr>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr>               <ord>    <int>    <int>                        <int>                    <int>
 1 Three    2019     7    22    12  24.6 -77.4 tropical depression -1          25     1013                            0                        0
 2 Three    2019     7    22    18  25   -78.4 tropical depression -1          25     1013                            0                        0
 3 Three    2019     7    23     0  25.7 -79.3 tropical depression -1          25     1013                            0                        0
 4 Three    2019     7    23     6  26.9 -79.6 tropical depression -1          30     1013                            0                        0
 5 Three    2019     7    23    12  28.6 -80   tropical depression -1          30     1014                            0                        0
 6 Fifteen  2019    10    14    12  12.6 -19.3 tropical depression -1          30     1006                            0                        0
 7 Fifteen  2019    10    14    18  13.3 -20   tropical depression -1          30     1006                            0                        0
 8 Fifteen  2019    10    15     0  14.2 -20.4 tropical depression -1          30     1006                            0                        0
 9 Fifteen  2019    10    15     6  15   -21   tropical depression -1          30     1006                            0                        0
10 Fifteen  2019    10    15    12  15.7 -21.7 tropical depression -1          30     1006                            0                        0
11 Fifteen  2019    10    15    18  16.3 -22.6 tropical depression -1          30     1007                            0                        0
12 Fifteen  2019    10    16     0  17   -23.4 tropical depression -1          30     1009                            0                        0
13 Ten      2020     7    31    18  15.5 -19.7 tropical depression -1          30     1008                            0                        0
14 Ten      2020     8     1     0  16.5 -20.3 tropical depression -1          30     1008                            0                        0
15 Ten      2020     8     1     6  17.4 -21   tropical depression -1          30     1008                            0                        0
16 Ten      2020     8     1    12  18.3 -21.8 tropical depression -1          25     1009                            0                        0
17 Ten      2020     8     1    18  19   -23   tropical depression -1          25     1009                            0                        0

$`tropical storm`
# A tibble: 362 × 13
# Groups:   name [23]
   name     year month   day  hour   lat  long status              category  wind pressure tropicalstorm_force_diameter hurricane_force_diameter
   <chr>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr>               <ord>    <int>    <int>                        <int>                    <int>
 1 Chantal  2019     8    20    18  40.1 -59.7 tropical depression -1          30     1008                            0                        0
 2 Chantal  2019     8    21     0  40.3 -57.1 tropical storm      0           35     1007                           40                        0
 3 Chantal  2019     8    21     6  40.3 -54.8 tropical storm      0           35     1007                           50                        0
 4 Chantal  2019     8    21    12  40.1 -52.5 tropical storm      0           35     1007                           50                        0
 5 Chantal  2019     8    21    18  39.9 -50.3 tropical storm      0           35     1007                           50                        0
 6 Chantal  2019     8    22     0  39.6 -48.3 tropical depression -1          30     1007                            0                        0
 7 Chantal  2019     8    22     6  39.3 -46.3 tropical depression -1          30     1007                            0                        0
 8 Chantal  2019     8    22    12  38.9 -44.4 tropical depression -1          30     1007                            0                        0
 9 Chantal  2019     8    22    18  38.5 -42.9 tropical depression -1          30     1007                            0                        0
10 Chantal  2019     8    23     0  38   -41.8 tropical depression -1          30     1007                            0                        0
# … with 352 more rows

Data

library(tidyverse)

# data(storms)
storms_19_20_21.df <- storms %>% 
  filter(year >= 2019)
Related