mercredi 18 juillet 2018

How to make factor names appear in ifelse statement in R?

I have to following dataset. I want to create a column so that if there is a number in the unid column then in dat$identification I want it to say "unidentified" otherwise I want it to print whatever is there in the species column. So the final output should look like dat$identificaiton x,y,unidentified,unidentified. With this code it shows 1,2,unidentified,unidentified.

Please note, for other purposes I want to use only the unid column for the !(is.na) part of the ifelse statement and not the species.

unid <- c(NA,NA,1,4)
species <- c("x","y",NA,NA)
df <- data.frame(unid, species)
df$identification <- ifelse(!is.na(unid), "unidentified", df$species)

#Current Output of df$identification: 
1,2,unidentified,unidentified

#Needed Output
x,y,unidentified,unidentified

Aucun commentaire:

Enregistrer un commentaire