I am trying to get the 3 most common numbers per group of a dataframe, using a function, but ignoring the less common values (per group), and allowing a unique number if present. Accepted answer will have the lowest system.time
#my current function
library(plyr)
get.3modes.andcounts<- function(origtable,groupby,columnname) {
data <- ddply (origtable, groupby, .fun = function(xx){
c(m1 = paste(names(sort(table(xx[,columnname]),decreasing=TRUE)[1])),
m2 = paste(names(sort(table(xx[,columnname]),decreasing=TRUE)[2])),
m3 = paste(names(sort(table(xx[,columnname]),decreasing=TRUE)[3])),
counts=length2(xx[[columnname]], na.rm=TRUE) #http://www.cookbook-r.com/Manipulating_data/Summarizing_data/
) } )
return(data)
}
length2 <- function (x, na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else length(x)
}
# example df
col2<-c(4, 4, 4, 4, 5, 3, 3, 3, 2, 2, # group1 "5" is the less common
2, 2, 2, 4, 4, 3, 3, 2, 2, 2, # group2 "3" and "4" are equally less common, and there is 2 more frequent
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, # group3 "4" is unique
4, 4, 4, 4, 5, 5, 5, 5, 2, 2, # group4 "2" is the less common, other ties more frequent
4, 4, 4, 4, 4, 5, 5, 5, 5, 5) # group5 "4" and "5" are equally common and no value is less common (similar to unique)
col1<-paste(c(rep("group1",10),rep("group2",10),rep("group3",10),rep("group4",10),rep("group5",10)), sep=", ")
df<-data.frame(col1=col1,col2=col2)
get.3modes.andcounts(df,"col1","col2")
#CURRENT result
col1 m1 m2 m3 counts
1 group1 4 3 2 10 # ok
2 group2 2 3 4 10 # no, 3 and 4 are the less common
3 group3 4 NA NA 10 # ok
4 group4 4 5 2 10 # no, 2 is less common
5 group5 4 5 NA 10 # ok
# desired
col1 m1 m2 m3 counts
1 group1 4 3 2 10
2 group2 2 NA NA 10
3 group3 4 NA NA 10
4 group4 4 5 NA 10
5 group5 4 5 NA 10
EDIT: The real sample has several ties, and having more than 3 columns is undesired. More than 3 numbers (in 3 columns) are accepted only if ties present. That is why, I decided to ask for another type of output.
EDIT: group7. Only three most common wanted. Exception, ties that include the 3rd most common (like in other groups).
# EXAMPLE 2
# new proposal
col2<-c(4, 4, 4, 4, 5, 3, 3, 3, 2, 2, 6, 6, # group1 2 and 6 tied in the 3rd position, 5 less common
2, 2, 2, 4, 4, 3, 3, 2, 2, 2, 6, 6, # group2 4, 3 and 6 tied in the less common, excluded.
4, 4, 4, 7, 7, 7, 5, 5, 5, 4, 4, 6, # group3 4, 7 and 5 more common, 3 most common present, exclude everything else
4, 4, 4, 4, 5, 5, 5, 5, 2, 2, 6, 6, # group4 2 and 6 less common, excluded (4 AND 5 tied)
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, # group5 6 less common, excluded, (4 and 5 tied)
4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, # group6 all tied
14,14,14,16,16,16,16,34,34,42,42,42,80,80,84,92, #group7 16, 14, 42 are the three most freq.
20,52,40,40,40,20,20,60,60,50) #group 8 20,40 tied, 60 next.
col1<-paste(c(rep("group1",12),rep("group2",12),rep("group3",12),rep("group4",12),rep("group5",12),
rep("group6",12),rep("group7",16),rep("group8", 10)), sep=", ")
df<-data.frame(col1=col1,col2=col2)
#desired output
col1 m1 m2 m3 counts
1 group1 4 3 2,6 12 # 2 and 6 tied in the 3rd position, 5 less common
2 group2 2 NA NA 12 # 4, 3 and 6 tied in the less common, excluded.
3 group3 4 7,5 NA 12 # three most common numbers present, exclude everything else
4 group4 4,5 NA NA 12 # 2 and 6 less common excluded (4 AND 5 tied)
5 group5 4,5 NA NA 12 # 6 less common, excluded, (4 and 5 tied)
6 group6 4,3,2,1 NA NA 12 # all tied
7 group7 16 14,42 NA 16 # three most frequent present, discard others
8 group8 20,40 60 NA 10 # three most frequent present