jeudi 16 novembre 2017

assign column value based on multiple conditional criteria

I have been trying to figure out how to assign a value based on some criteria.

I have a dataset that simplistically looks like:

fish <- data.frame ("method"=c("cryptic", "cryptic", "cryptic", 
                               "quad", "quad", "quad", 
                               "fish", 
                               "1mquad", "1mquad"),
                    "species"= c("A", "B", "C", 
                                 "A", "B", "D", 
                                 "A",
                                 "A", "B"), 
                  "cut"=1)

I want to change the value of cut to 0 if a species has both the method cryptic and quad or cryptic and 1mquad.

I've tried a few different things and keep getting stuck. When I do an if statement I get weird warnings. Some things I've tried are making a subset df with just cryptic

cryptic <- fish [fish$method == "cryptic",]

and then trying to subset

fish$cut [match(fish$species, cryptic$species) & fish$method =="quad"] <- 0
fish$cut [match(fish$species, cryptic$species) & fish$method =="1mquad"] <- 0

but that doesn't work. I also tried creating conditions:

cond3 <- fish$species %in% intersect(fish$species, cryptic$species)
cond4 <- fish$method %in% c ("1mquad", "quad", "cryptic")

and doing an if statement with that, but to no avail. Also, I have a few other conditions prior that changes cut to 0, so I can't use an ifelse statement because it overwrites those other conditions. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire