count values that are equal from 2 columns

column 1 : c(1,2,5,6)
column 2 : c(5,6,10,11)

i want to know how many (count) values from the two columns that is identical (so 5 and 6 is in both columns)

ps. in pandas it is "isin" but dont know about R :slight_smile:

This isn't too tricky to do, its just knowing which function to use!

one = c(1,2,5,6)
two = c(5,6,10,11)

common = intersect(one, two)

length(common)

intersect() returns a vector containing common values in two vectors. The length of that is therefore the number of common items.

If these vectors are columns in a data frame, it looks very similar:

dat = data.frame(
  one = c(1,2,5,6),
  two = c(5,6,10,11)
)

common = intersect(dat$one, dat$two)

length(common)

Another alternative

one = c(1,2,5,6)
two = c(5,6,10,11)

sum(one %in% two)
#> [1] 2

Created on 2021-12-01 by the reprex package (v2.0.1)

2 posts were split to a new topic: Help merging two data frames by a new column that doesn't share the same name

This topic was automatically closed 7 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.