3

I have a data like original with much more columns.

id <- c('A','B','C', 'D', 'E', 'F', 'G')
month <- c('NA', 'D', 'H', 'I', 'A', 'B', 'NA')
iso <- c('NA', 'NA', 'NA', 'A', 'B', 'C', 'NA')
original <- data.frame(id, month, iso)

I want to create a string containing all common elements found in the columns, like string common:

common <- c("A", "B")

I have found posts like: R: How can I find the intersection of elements from two rows of a dataframe? or like: How to find common elements from multiple vectors?

But these posts do not make the trick. In such a high-dimensional dataset I need something "less manual".

Any clue?

Thank you

vog
  • 770
  • 5
  • 11

4 Answers4

3

One options could be:

Reduce(`intersect`, original)

[1] "A" "B"
tmfmnk
  • 38,881
  • 4
  • 47
  • 67
2

Try the code below

library(RVenn)
overlap(Venn(original))
# [1] "A" "B"
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
2

Using purrr

library(purrr)
reduce(original, intersect)
#[1] "A" "B"
akrun
  • 874,273
  • 37
  • 540
  • 662
1
  1. convert data into a list (actually the dataset was a list originally)
  2. then:
unli_df <- unlist(df)
unique(unli_df)
vog
  • 770
  • 5
  • 11