I'm trying to generate a column in my data frame, let us say it is called 'Status' and it should provide the status of the fish i.e. protected or unprotected.
What I am looking for:
ID Species Status
1 1 Epinephelus polyphekadion Protected
2 2 Epinephelus tukula Protected
3 3 Thunnus albacares Protected
4 4 Sphyraena barracuda Unprotected
5 5 Lutjanus rivulatus Unprotected
6 6 Lethrinus lentjan Unprotected
7 7 Plectropomus pessuliferus Protected
My Data:
fishydata <- structure(list(ID = 1:7, Species = structure(c(1L, 2L, 7L, 6L,4L, 3L, 5L), .Label = c("Epinephelus polyphekadion", "Epinephelus tukula","Lethrinus lentjan", "Lutjanus rivulatus", "Plectropomus pessuliferus","Sphyraena barracuda", "Thunnus albacares"), class = "factor"), .Names = c("ID", "Species"), row.names = c(NA, 7L), class = "data.frame")
The data set contains over a 1000 observations. Is their a line of code that can link specific species to a status in a new column.
I have over 40 species and 7 are protected. I'm looking to provide the 7 species with a 'Protected' status and disregard everything else as 'Unprotected' rather than typing out all the species names and classing them as 'Unprotected'
Any pointers or advice would be greatly appreciated. My skills are basic, attempting to get back into R. I've been dabbling in dplyr using mutate and filter but I've reached a brick wall.
Your data without the Status column :
fishydata2 <- structure(list(ID = 1:7,
Species = structure(c(1L, 2L, 7L, 6L,4L, 3L, 5L),
.Label = c("Epinephelus polyphekadion", "Epinephelus tukula","Lethrinus lentjan", "Lutjanus rivulatus", "Plectropomus pessuliferus","Sphyraena barracuda", "Thunnus albacares"), class = "factor")
),
.Names = c("ID", "Species"),
row.names = c(NA, 7L),
class = "data.frame")
# ID Species
#1 1 Epinephelus polyphekadion
#2 2 Epinephelus tukula
#3 3 Thunnus albacares
#4 4 Sphyraena barracuda
#5 5 Lutjanus rivulatus
#6 6 Lethrinus lentjan
#7 7 Plectropomus pessuliferus
You just have to create a new column with an Unprotected status by default:
fishydata2$Status <- "Unprotected"
And now, just update it for your only 7 protected species:
fishydata2[fishydata2$Species %in% c('Epinephelus polyphekadion',
'Epinephelus tukula','Thunnus albacares',
'Plectropomus pessuliferus'),]$Status <- "Protected"
Results:
fishydata2
#ID Species Status
#1 1 Epinephelus polyphekadion Protected
#2 2 Epinephelus tukula Protected
#3 3 Thunnus albacares Protected
#4 4 Sphyraena barracuda Unprotected
#5 5 Lutjanus rivulatus Unprotected
#6 6 Lethrinus lentjan Unprotected
#7 7 Plectropomus pessuliferus Protected