R: count the number of occurrences -
this question has answer here:
- is there built-in function finding mode? 26 answers
i more familiar python need in r. have data frame this:
id apps 8400 10,19,9,9,8,9,1,3,3,6 10915 10,2,6,2,3,2,2,3,2,3,2,6 72331 10,9,6,1,2,4,6,2,14,3,3,2,3,9,2
i want count number of occurrences of each app , return app occurrences in new column:
id apps 8400 10,19,9,9,8,9,1,3,3,6 9 10915 10,2,6,2,3,2,2,3,2,3,2,6 2 72331 10,9,6,1,2,4,6,2,14,3,3,2,3,9,2 2
bests.
i added answer case, maybe helps else :).
let me answer question, maybe helps else too:
mymode <- function(x) { x <- strsplit(x,",") names(sort(-table(x)))[1] }
then apply it:
df$most <- lapply(as.character(test$apps),mymode)
Comments
Post a Comment