I have a data set for example,
Data <- data.frame(
groupname = as.factor(sample(c(\"a\", \"b\", \"c\"), 10, replace = TRUE)),
someuser = sample(c(\"x\
This might work for you - using base R
set.seed(1)
Data <- data.frame(
groupname = as.factor(sample(c("a", "b", "c"), 10, replace = TRUE)),
someuser = sample(c("x", "y", "z"), 10, replace = TRUE))
Data
groupname someuser
1 a x
2 b x
3 b z
4 c y
5 a z
6 c y
7 c z
8 b z
9 b y
10 a z
res <- lapply(split(Data, Data$groupname), function(x)
data.frame(groupname=x$groupname[1], someuser=names(sort(table(x$someuser),
decreasing=TRUE))[1]))
do.call(rbind, res)
groupname someuser
a a z
b b z
c c y
And using ddply
sort_fn2 <- function(x) {names(sort(table(x$someuser), decreasing=TRUE))[1]}
ddply(Data, .(groupname), .fun=sort_fn2)
groupname V1
1 a z
2 b z
3 c y