samedi 25 mars 2017

Determine if Value in Final Column exists in respective rows

I have a dataframe as follows:

df1

ColA     ColB    ColC    ColD     ColE     COlF    ColG      Recs
   1      A-1   A - 3       B        B       NA                 C
   1              B-1     C R        D        E      NA         B
   1       NA       A       B        A                          B

How do I determine if the last from the column Recs is found in it's respective row?

I tried below but it doesn't work because there are duplicates in my normal dataset:

df1$Exist <- apply(df1, 1, FUN = function(x) 
            c("No", "Yes")[(anyDuplicated(x[!is.na(x) & x != "" ])!=0) +1])

There are also blanks, NA's, and character values that have spaces and dashes.

Final output should be:

ColA     ColB    ColC    ColD     ColE     COlF    ColG      Recs    Exist?
   1      A-1   A - 3       B        B       NA                 C        No
   1              B-1     C R        D        E      NA         B        No
   1       NA       A       B        A                          B       Yes

Thanks

Aucun commentaire:

Enregistrer un commentaire