Hi, I'm new with R and in the forum, I know that probably it is easy to get the answer to my question but I didn't find how to look for it.
I will use an example to explain my doubts...
I have a document with two columns,
Column1:Matias, Pepe, Jorge, Elias, Paloma, Paula, estela
Column2:Paula, Javi, Ulises, Carlos, Pepe
What I want to do with R is to get the matches of both colums, the output I will want to get is "Paula, Pepe" which are the names that are in both columns, I only found the code to see if they are repeated in the same row using == but I would like to get even if one is in column1 row 3 and the other in column2 row 45.
Using match() with both columns I can get where the matches are but I would like to get which are the names that are repeated.
Thanks!
Does this do what you intended?
library(tidyverse)
col1 <- as.character(expression(Matias, Pepe, Jorge, Elias, Paloma, Paula, Estela))
col2 <- as.character(expression(Paula, Javi, Ulises, Carlos, Pepe, Paco, Pedro))
dframe <- tibble(col1,col2)
dframe
#> # A tibble: 7 x 2
#> col1 col2
#> <chr> <chr>
#> 1 Matias Paula
#> 2 Pepe Javi
#> 3 Jorge Ulises
#> 4 Elias Carlos
#> 5 Paloma Pepe
#> 6 Paula Paco
#> 7 Estela Pedro
dframe %>%
mutate(col1_dupe = !is.na(match(col1, col2))) ->
dframe2
dframe2
#> # A tibble: 7 x 3
#> col1 col2 col1_dupe
#> <chr> <chr> <lgl>
#> 1 Matias Paula FALSE
#> 2 Pepe Javi TRUE
#> 3 Jorge Ulises FALSE
#> 4 Elias Carlos FALSE
#> 5 Paloma Pepe FALSE
#> 6 Paula Paco TRUE
#> 7 Estela Pedro FALSE
dframe2$col1[dframe2$col1_dupe]
#> [1] "Pepe" "Paula"
Created on 2020-12-21 by the reprex package (v0.3.0)
Yes! Thanks for your time.
Took me a minute, but if you prefer a tidy/pipy approach, this works also:
library(tidyverse)
col1 <- as.character(expression(Matias, Pepe, Jorge, Elias, Paloma, Paula, Estela))
col2 <- as.character(expression(Paula, Javi, Ulises, Carlos, Pepe, Paco, Pedro))
dframe <- tibble(col1,col2)
dframe
#> # A tibble: 7 x 2
#> col1 col2
#> <chr> <chr>
#> 1 Matias Paula
#> 2 Pepe Javi
#> 3 Jorge Ulises
#> 4 Elias Carlos
#> 5 Paloma Pepe
#> 6 Paula Paco
#> 7 Estela Pedro
dframe %>%
mutate(col1_dupe = !is.na(match(col1, col2))) %>%
mutate(result = if_else(condition = col1_dupe,
true = col1,
false = NA_character_)) %>%
na.omit() %>%
pull(result)
#> [1] "Pepe" "Paula"
Created on 2020-12-21 by the reprex package (v0.3.0)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.