how can I select and extract a biomass value based on the name of the corresponding fish ?
fish_data <- data.frame(names = c("Fish_1", "Fish_2", "Fish_3", "Fish_4", "Fish_5"),
biomass = c(1.532, 2.243, 1.325, 1.098, 4.347))
##################
names biomass
1 Fish_1 1.532
2 Fish_2 2.243
3 Fish_3 1.325
4 Fish_4 1.098
5 Fish_5 4.347
CodePudding user response:
In addition to Sotos' answer, you have dplyr::filter
.
dplyr::filter(fish_data, names == "Fish_1")$biomass
This method can be useful for identifying several values and/or use several logical filters, in a more concise and flexible way. For example you can retrieve all the "biomass" values of the observations having "Fish" or "fish" in their "names" with
dplyr::filter(fish_data, stringr::str_detect(names,"Fish|fish"))$biomass