简体   繁体   中英

Counting unique pairs of data R

I have a following data set

data1 = data.frame("Element" = sample(c(1:100), 600, replace = T))
data1$Factor2 = sample(c("E", "F", "G"), 600, replace = T)

I'd like to count the number of Elemens that got matched with each factor from Factor2. For example, an output could like like the following table:

Factor Number of elements

E     45
F     67
G     34

which would mean there are 45 distinct rows such as: E,1;E11;E:20. Although row E,1 appears more times, I am not interested in how many times each combination appears, I am interested in how many unique combinations there were.

You can use unique to get the distinct rows and then just create a table of how many times each factor occurred. I am setting the seed to make the data reproducible.

set.seed(2018)
data1 = data.frame("Element" = sample(c(1:100), 600, replace = T))
data1$Factor2 = sample(c("E", "F", "G"), 600, replace = T)

table(unique(data1)$Factor2)
 E  F  G 
85 92 79 

An option with dplyr

library(dplyr)
data1 %>% 
    distinct() %>% 
    count(Factor2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM