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

Keep columns with matching rows

In R, I have a dataframe with e.g. 4 variables:

df <- data.frame(
  v1=c(1,2,3,4),
  v2=c("x","y","z","q"),
  v3=c("x","b","c","d"),
  v4=c("a","y","c","d"),
  v5=c("a","b","z","d"),
  v6=c("a","b","c","q")
)

Suppose I use v2 as reference and I want to know what other columns match the values of v2.

How do I match the values from v3, v4, etc… to v2, in such a way that I know from which column the match came?
The result would look something like:

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

Var 1 Var 2 match
1 x v3
2 y v4
3 z v5
4 q v6

I tried match, %in%, and creating matrices of combinations, however I could not find the solution.

>Solution :

You could try:

library(tidyverse)

ref_col <- 'v2'

df %>%
  pivot_longer(-c('v1', ref_col)) %>%
  group_by(across(c('v1', ref_col))) %>%
  summarise(match = name[as.character(.data[[ref_col]]) == as.character(value)])

Output:

# A tibble: 4 x 3
# Groups:   v1 [4]
     v1 v2    match
  <dbl> <fct> <chr>
1     1 x     v3   
2     2 y     v4   
3     3 z     v5   
4     4 q     v6  
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