Chuck C
Chuck C

Reputation: 153

find rows that are the same as a vector in R

I want to search row by row, and if it matches a pre defined vector, assign a value to a variable of that row. I prefer to solve it by using dplyr to stay in the pipeline.

for a simplified example:

a=c(1,2,NA)
b=c(1,NA,NA)
c=c(1,2,3)
d=c(1,2,NA)
D= data.frame(a,b,c,d)

My attempt is:

D %>% mutate(
            i= case_when(
                 identical(c(a,b,c),c(1,1,1)) ~ 1,
                 identical(c(a,b,c),c(NA,NA,3)) ~ 2
                         )
             )

I hope it gives me:

   a  b c  d  i
1  1  1 1  1  1
2  2 NA 2  2  NA
3 NA NA 3  NA 2

but my code doesn't work I guess it's because it's not comparing a row to a vector.

I do not want to simply type within the case_when c==1 & b==1 & c== 1 ~ 1 because there will be too many variables to type in my dataset.

Thank you for your advise.

Upvotes: 1

Views: 179

Answers (2)

akrun
akrun

Reputation: 886938

If we have multiple conditions, create a key/value dataset and then do a join

library(dplyr)
keydat <- data.frame(a =c(1, NA), b = c(1, NA), c = c(1, 3), i = c(1, 2))
left_join(D, keydat)
#   a  b c  d  i
#1  1  1 1  1  1
#2  2 NA 2  2 NA
#3 NA NA 3 NA  2

Upvotes: 1

Emily Kothe
Emily Kothe

Reputation: 872

For this example

The following code would work

a=c(1,2,NA)
b=c(1,NA,NA)
c=c(1,2,3)
D= data.frame(a,b,c,d)


  D %>% mutate(
    i= case_when(
      paste(a,b,c, sep=',') == paste(1,1,1, sep=",") ~ 1,
      paste(a,b,c, sep=',') == paste(NA,NA,3, sep=",") ~ 2
    )
  )

   a  b c  d  i
1  1  1 1  1  1
2  2 NA 2  2 NA
3 NA NA 3 NA  2

Upvotes: 2

Related Questions