lundi 5 décembre 2016

create a new column and enter a 1 or 0 based on the conditions provided

test1 <- data.frame( D= c("gt","gt","gt", "bk", "j","pro", "r","ll", "oo", "r","bk","oo", 
                          "gt", "bk", "j","oo", "j","ll", "oo", "r", "gt", "bk", "j","oo"),
            NM = c("jaes","jan ","tommy","joe","eleder","food","drink","elephant","jaes","jan","tommy","joe",
                       "jaes","jan","drink","elephant","jaes","jelly", "jelly", "ranch","food","drink","jaes","jan "))

test2 <- data.frame(p1= c("jaes","tommy","joe","eleder","food","jelly"), 
            p2 = c("jelly","joe","NA","NA","A","jelly"),
            oi =c(1,2,3,4,5,6))

  1. I want to create a new column in data frame res_test named "NC" that will consist of 0 or 1 based on the conditions below
  2. for every name(s) in column "p1" or "p2" check if either of the name(s) exist in column "NM"
  3. if the names do not exist enter 0 in column "NC" for that party(s)
  4. for each name(s) from "p1" that exist in "NM" check the corresponding cell from "D" to see if the string "gt" exists
  5. if the string exist enter a 1 in the "NC" column for that name(s) if not enter 0

Aucun commentaire:

Enregistrer un commentaire