I need to understand where I'm making mistakes. I've googled and tried many approaches but I didn´t get the desired result. I addition, I'm new to writing R code.
What I want to do is to replace in column “sp” the scientific name of “Gaidropsaurus vulgaris” to “Gaidropsaurus macrophtalma” or viceversa, based on a concrete depth range (>=148 m) and in the condition is false, leave Gaidropsaurus alone. In addition, the rest of the species should stay untouched (I didn´t get this point neither). I guess I need to create a nested? ifelse statement. The idea is: ifelse (dfrm$depth >=148, "Gaidropsaurus macrophthalmus", "Gaidropsaurus vulgaris")
I have a database with 3 variables: year, species and depth.
year <- c("2000", "2000", "1999", "1999", "2000", "2000", "1998", "1998", "2000", "1998", "2000", "1998", "1999", "2000", "2000", "1999", "1999", "2000", "1998", "1998", "1998", "2000", "1998", "1999", "1998", "2000", "1998", "1999", "1999", "2000", "2000", "1999", "1999")
sp <- c("Merluccius merluccius", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Merluccius merluccius", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Gadiculus argenteus", "Gadiculus argenteus", "Micromesistius poutassou", "Micromesistius poutassou", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Merluccius merluccius", "Merluccius merluccius", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Gadiculus argenteus", "Gadiculus argenteus", "Micromesistius poutassou", "Micromesistius poutassou", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Micromesistius poutassou", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma", "Gaidropsaurus vulgaris", "Gaidropsaurus macrophtalma")
depth <- c("77", "111", "456", "123", "389", "211", "244", "176", "198", "88", "118", "96", "117", "256", "193", "379", "201", "144", "193", "137", "188", "256", "193", "379", "201", "345", "100", "139", "222", "345", "675", "67", "177")
dfrm <- data.frame (year, sp, depth)
The problem is that all the approaches I tried didn´t leave the other species alone and changed the other name species to numbers. I know that I could subset Gaidropsaurus species, change the “sp” value using ifelse and then merge it with the rest of the species, but I'm sure there's a better, faster or more efficient way of doing this. I hope I have made myself clear. Any ideas?
Aucun commentaire:
Enregistrer un commentaire