1

I currently have a dataframe called countries with 2 columns "V1" and "V2." Both columns contain country abbreviations such US, CA, RU etc. Each row is a pair of countries so the first row can be US and RU. However, I want to extract unique pairs since some pairs are repeated such as (US,RU) and (RU,US). I want to extract the pair according to alphabetical order so (RU,US) would be extracted, not (US, RU).

Currently I have:

countries = filter(countries, countries$V1 < countries$V2)

However, this is causing an error.

Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "c('matrix', 'array', 'character')"

I'm assuming there is an issue with how I am using the filter function to take out pairs according to alphabetical order. Is there a different way to do this?

Angie
  • 183
  • 3
  • 13

2 Answers2

1

Here's one approach with some example data:

library(tidyverse)

countries <- data.frame(V1 = c("US", "MX", "CA", "UR", "US"),
                        V2 = c("MX", "FR", "IN", "US", "MX"),
                        Combo = NA)

for(i in 1:nrow(countries)){
      countries$Combo[i] <- paste0("(", 
                                   str_c(sort(c(countries$V1[i], countries$V2[i])),
                                         collapse = ", "),
                                   ")")
}

countries %>% pull(Combo) %>% unique()
shirewoman2
  • 1,842
  • 4
  • 19
  • 31
0

You can try using

distinct_all(countries)
writer_typer
  • 708
  • 7
  • 25
  • I am currently getting this error when I try to use that function ```Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "c('matrix', 'array', 'character')"``` Does my data need to be transformed in some way? – Angie Jul 16 '20 at 17:21
  • Can you please share example data from your dataset? – writer_typer Jul 16 '20 at 17:22
  • 1
    I was just able to figure out the issue, thank you! – Angie Jul 16 '20 at 17:23