
#changing order
sex<-factor(sex, 
            levels = c("M", "F", "other")
)
summary(sex)

#using dplyr
dplyr_sex<-recode_factor(surveys$sex, 
                         "M" = "M", "F" = "F", .default = "other")
summary(dplyr_sex)

surveys$sex<-dplyr_sex
summary(surveys$sex)

#Combining columns                                       
surveys<-unite(data = surveys, #your data frame
               col = "lat_long", #new column name
               decimalLatitude, decimalLongitude, #columns you want to merge 
               sep= ",") #what you want to separate the values with

class(surveys$lat_long)

surveys$lat_long<-surveys$lat_long

summary(surveys$lat_long)

#Fix the NA values
surveys$lat_long[surveys$lat_long == "NA,NA"]<-NA


######################### Filtering ############################################
#by a factor
surveys_DM<-surveys[surveys$species=="DM",]

#by a number
surveys_hfl<-surveys[tf, ]

tf<-surveys$hfl>60

#by a character string
hawaii_pos<-grep(pattern = "Hawaii", #what you want to find
                 x = surveys$locality       #the column you're searching
) #returns positions

surveys_hawaii<-surveys[hawaii_pos,]

########################## Sorting ############################################
# sort by hfl
hfl_sort <- surveys[order(surveys$hfl),] 

# sort by hfl and wgt
hfl_wt_sort <- surveys[order(surveys$hfl, surveys$wgt),]

#sort by hfl (ascending) and wgt (descending)
hfl_wt_sort <- surveys[order(surveys$hfl, -surveys$wgt),]
