I am a beginner in R, and I have a table that consists of two columns. The first column is the company name. The second one is the share class. Most of the time, companies have one class of shares. But I want get a list of companies with multiple share classes (say, two share classes).
Given this table
in this case, I want to get
Result
Thank you very much!
CodePudding user response:
You could do:
names(which(rowSums(table(df))>1))
[1] "3" "6" # Companies 3 and 6 have more than 1 share class
or even
subset(aggregate(.~company, unique(df), length), `share class`>1)
company share class
3 3 2
5 6 2
df <- structure(list(company = c(1L, 2L, 3L, 3L, 4L, 6L, 6L), `share class` = c("a",
"a", "a", "b", "a", "a", "b")), class = "data.frame", row.names = c(NA,
-7L))