There are several ways to detect and count common elements in R, depending on the data structure that contains the elements. Here are a few examples:
vec1 <- c(1, 2, 3, 4)
vec2 <- c(3, 4, 5, 6)
common <- intersect(vec1, vec2)
count <- length(common)
list1 <- list(a = c(1, 2, 3), b = c(3, 4, 5))
list2 <- list(a = c(3, 4, 5), b = c(5, 6, 7))
common <- lapply(seq_along(list1), function(i) intersect(list1[[i]], list2[[i]]))
count <- sapply(common, length)
df <- data.frame(x = c(1, 2, 3, 4), y = c(3, 4, 5, 6))
common <- merge(df[df$x %in% df$y, "x"], df[df$y %in% df$x, "y"])
count <- nrow(common)
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-07-30 11:00:00 +0000
Seen: 13 times
Last updated: Sep 13 '22
How can I use Prometheus to count the number of distinct label values?
How to count relationship column in Laravel using GroupBy?
What is the total count of unique arrangements of key values that can be inserted into a hash table?
How can ReceivedTime in Outlook VBA be utilized to track the number of emails received yesterday?
How to count the total number of exported identifiers using the eslint rule?
What is the method to execute React useEffect hook just once when there is a modification in state?
What is the method to apply two aggregation operations to lookup the same model?