naomifk / mini_proj_3

Mini project 3!
0 stars 0 forks source link

New column with difficulty rating #1

Open naomifk opened 5 years ago

naomifk commented 5 years ago

Our previous plan, to use difficulty_function for each trail and then try to add the results to the existing dataframe, won't work because difficulty_function can't work with vectors.

@aallen00 I am going to try to scrap difficulty_function and make it a direct mutate onto the dataframe, if you want to try that as well.

Below I'm pasting the function in case we want to go back to using it or refer back to it later.

#remove geometry from trails df
trails_df <- `st_geometry<-`(trails_grouped, NULL)

#function to define trail difficulty according to SHD
difficulty_function <- function(trail_arg) {
  #select and filter so only SHD_number remains
 SHD_num <- trails_df %>%
    filter(name == trail_arg) %>% 
      select(SHD_number) %>% 
   #convert from tibble to vector
    pull(SHD_number)
 #if/else statements to define difficulty
 if(SHD_num<50) {
   return("Easy")
 }
 else if (SHD_num>=50 & SHD_num<100){
   return("Moderate")
 }
 else {
   return("Difficult")
 }
}

difficulty_function("Snowmobile Trail")
difficulty_function(c("Driveway", "Easy Out"))

trails_grouped_mutate <- trails_grouped %>% 
  mutate(difficulty = difficulty_function(name))