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.
- How could the code be written in a more efficient way using status?
- 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")