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:
| 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