I have one data frame about animal sightings (more than 300), with species of whales, dolphins, pinipedes and penguins.
And I want to create a new column reino
, which would be misticeto
for whales, odontoceto
for dolphins, pinipede
for pinipedes and penguin
for penguins.
But when I do that, one specific whale (Ballena franca austral (Eubalaena australis)) result in NA
value at reino
column.
library(data.table)
library(dplyr)
> packageVersion("dplyr")
[1] ‘1.1.4’
dt = data.table(especie= c('Ballena franca austral (Eubalaena australis)','Ballena barbada no identificada (Parvorden Mysticeti)', 'Ballena jorobada (Megaptera novaeangliae)', 'Ballena rorcual no identificada (Balaenoptera sp.)', 'Ballena sei (Balaenoptera borealis)', 'Ballena fin (Balaenoptera physalus)', 'Ballena barbada no identificada(Parvorden Mysticeti)','Delfín austral (Lagenorhynchus australis)', 'Delfín no identificado (Familia Delphinidae)', 'Tonina overa (Cephalorhynchus commersonii)', 'Delfín oscuro (Lagenorhynchus obscurus)','Lobo marino de dos pelos no identificado (Arctocephalus sp.)', 'Lobo marino no identificado (Familia Otariidae)', 'Lobo marino de dos pelos sudamericano (Arctocephalus australis)', 'Lobo marino de un pelo sudamericano (Otaria flavescens)', 'Lobo marino de dos pelos antártico (Arctocephalus gazella)','Lobo marino de dos pelos sudamericano (Arctocephalusaustralis)','Pingüino patagónico (Spheniscus magellanicus)', 'Pingüino no identificado (Familia Spheniscidae)', 'Cormorán Imperial (Phalacrocorax albiventer)', 'Pingüino penacho amarillo austral (Eudyptes chrysocome)', 'Pingüino rey (Aptenodytes patagonicus)'))
as.character(dt$especie)
I want do do this:
dt$reino[dt$especie %in% c('Ballena franca austral (Eubalaena australis)','Ballena barbada no identificada (Parvorden Mysticeti)', 'Ballena jorobada (Megaptera novaeangliae)', 'Ballena rorcual no identificada (Balaenoptera sp.)', 'Ballena sei (Balaenoptera borealis)', 'Ballena fin (Balaenoptera physalus)', 'Ballena barbada no identificada(Parvorden Mysticeti)')] <- 'misticeto'
dt$reino[dt$especie %in% c('Delfín austral (Lagenorhynchus australis)', 'Delfín no identificado (Familia Delphinidae)', 'Tonina overa (Cephalorhynchus commersonii)', 'Delfín oscuro (Lagenorhynchus obscurus)')] <- 'odontoceto'
dt$reino[dt$especie %in% c('Lobo marino de dos pelos no identificado (Arctocephalus sp.)', 'Lobo marino no identificado (Familia Otariidae)', 'Lobo marino de dos pelos sudamericano (Arctocephalus australis)', 'Lobo marino de un pelo sudamericano (Otaria flavescens)', 'Lobo marino de dos pelos antártico (Arctocephalus gazella)','Lobo marino de dos pelos sudamericano (Arctocephalusaustralis)')] <- 'pinipede'
dt$reino[dt$especie %in% c('Pingüino patagónico (Spheniscus magellanicus)', 'Pingüino no identificado (Familia Spheniscidae)', 'Cormorán Imperial (Phalacrocorax albiventer)', 'Pingüino penacho amarillo austral (Eudyptes chrysocome)', 'Pingüino rey (Aptenodytes patagonicus)')] <- 'penguin'
When I run the code for the first time came this warning:
Warning message: Unknown or uninitialised column: reino.
Then, I continue running the code, and the new column reino
came with NA
value only for Ballena franca austral (Eubalaena australis).
I tried to do these things, but didn’t work:
dt %>% left_join(data.frame(especie = c('Ballena franca austral (Eubalaena australis)', reino = c('misticeto'))))
and
mutate(reino = case_match(especie, 'Ballena franca austral (Eubalaena australis)' ~ 'misticeto'))
The second problem is that:
- I noticed that one dolphin and one penguin came to the
misticeto
(whale) condition.
misticeto<-filter(dt, reino == "misticeto");misticeto
> table(misticeto$especie)
Ballena barbada no identificada (Parvorden Mysticeti) Ballena barbada no identificada(Parvorden Mysticeti)
23 1
Ballena fin (Balaenoptera physalus) Ballena franca austral (Eubalaena australis)
4 33
Ballena jorobada (Megaptera novaeangliae) Ballena rorcual no identificada (Balaenoptera sp.)
13 9
Ballena sei (Balaenoptera borealis) Delfín oscuro (Lagenorhynchus obscurus)
8 1
Pingüino rey (Aptenodytes patagonicus)
1
#The Delfín oscuro (Lagenorhynchus obscurus) and Pingüino rey (Aptenodytes patagonicus) shouldn't be there.
- And also, not all dolphins are going to the
reino
column, one is missing (who came tomisticeto
condition).
odontocetos<-filter(dt, reino == "odontoceto");odontocetos
> table(odontocetos$especie)
Delfín austral (Lagenorhynchus australis) Delfín no identificado (Familia Delphinidae)
25 12
Tonina overa (Cephalorhynchus commersonii)
8
#The dolphin that came to misticeto condition is missing here, 'Delfín oscuro (Lagenorhynchus obscurus)'
Someone can help me, please? I don’t understand what is happening.
Thanks!