An approach with plyr
:
myfun <- function(x) {
tab <- table(x)
idx <- max(tab) == tab
if (sum(idx) > 1)
"x"
else
names(tab)[idx]
}
library(plyr)
ddply(mydf, .(no), summarise,
article = paste(article, collapse = " "),
class = myfun(class))
Result:
no article class
1 3 earnings went up. costs were reduced. p
2 5 massive layoff. they moved their offices. Mr. X joined the company. x
3 24 class action filed. accident in warehouse. n
4 35 blabla one. blabla two. x
5 41 blabla three. blabla four. blabla five. p