Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

row 1 col 1 compare to row 3 col 3

I am having trouble finding this kind of solution. I am need to compare two columns but not in the same row in a data frame. Say the dataframe is this:

df <- data.frame (first_column = c("value_1", "value_5", "value_9", "value_13"),
              second_column = c("value_2", "value_6", "value_10", "value_14"),
              third_column = c("value_3", "value_7", "value_1", "value_15"),
              fourth_column = c("value_4", "value_8", "value_12", "value_16")
              )

I am wanting to compare col 1 to col 3 and see if there are any matches but not in the same row. And then mark or return the rows that have matching values.

Desired output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  first_column second_column third_column fourth_column match
1      value_1       value_2      value_3       value_4 TRUE
2      value_5       value_6      value_7       value_8 FALSE
3      value_9      value_10      value_1      value_12 TRUE
4     value_13      value_14     value_15      value_16 FALSE

or

  first_column second_column third_column fourth_column
1      value_1       value_2      value_3       value_4
3      value_9      value_10      value_1      value_12

>Solution :

df$match <- df$first_column %in% df$third_column | df$third_column %in% df$first_column
df

#>   first_column second_column third_column fourth_column match
#> 1      value_1       value_2      value_3       value_4  TRUE
#> 2      value_5       value_6      value_7       value_8 FALSE
#> 3      value_9      value_10      value_1      value_12  TRUE
#> 4     value_13      value_14     value_15      value_16 FALSE

Created on 2022-03-22 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading