In my work, I need to assign a score to a new column. The numeric value of this score is species specific.
Currently I have the following method to achieve this which works but is not very concise for repeated use with multiple data sets:
bird$VIS <- 0 # creates the new column and populates it with 0
bird$VIS[bird$species == "Tyto alba" ] <- 0.0502 # assigns this score to the VIS column for rows where the species is "Tyto alba"
bird$VIS[bird$species == "Branta leucopsis" ] <- 0.044
bird$VIS[bird$species == "Ciconia nigra" ] <- 0.002
bird$VIS[bird$species == "Grus grus" ] <- 0.001
bird$VIS[bird$species == "Bubo bubo" ] <- 0.004513
bird$VIS[bird$species == "Neophron percnopterus" ] <- 0.0015333
bird$VIS[bird$species == "Platalea leucorodia" ] <- 0.001
And so forth, there are 26 species in total but this subsample should be sufficient to demonstrate what I am trying to do.
My question is essentially how I turn this into a function that will work regardless of whether all the species are present in the data frame or not?
Essentially, instead of using the above sequential row assignments I would like to be able to write something like:
assign_VIS_function(bird)
resulting in an output something like:
SPECIES VIS
Branta leucopsis 0.044
Tyto alba 0.0502
Tyto alba 0.0502
Tyto alba 0.0502
Tyto alba 0.0502
Gyps fulvus 0.22838
Gyps fulvus 0.22838
Gyps fulvus 0.22838
and so forth......
Many thanks.