Home > Enterprise >  How can I get matches in columns (even if they are not in the same row number) in r?
How can I get matches in columns (even if they are not in the same row number) in r?

Time:04-20

Hi and thanks for reading me. I have a dataset that contains 3 vectors, and I want to compare the 2 first vectors. The idea is this: If I have at least one value in the first column that matches the value in the second column, then I would like to create a new variable that takes the value in the third column (which matches the value in the second column). I'm not quite sure how to do it, I tried the any() function but it doesn't work as I would like

For example, I have the following data frame:

x <- 
    data.frame(
      val1 = c(10,10,10,1,12,15),
      val2 = c(5,4,3,2,1,6),
      val3 = c(100,200,200,100,400,411)
    ) 

and I expected to get the following output (because the number 1 exists in the 2 first columns):

data.frame(
          val1 = c(10,10,10,1,12,15),
          val2 = c(5,4,3,2,1,6),
          val3 = c(100,200,200,100,400,411),
          val4 = c(NA,NA,NA,NA,400,NA)
        ) 

Thanks for the help

CodePudding user response:

You can use %in% to identify where val2 matches val1.

library(dplyr)

x %>%
  mutate(val4 = ifelse(val2 %in% val1, val3, NA))

  val1 val2 val3 val4
1   10    5  100   NA
2   10    4  200   NA
3   10    3  200   NA
4    1    2  100   NA
5   12    1  400  400
6   15    6  411   NA
  • Related