R: how to find the mode of a vector -


this question has answer here:

below data.frame , know mode each of memory categories (1 through 8)

> dput(d) structure(list(memory1 = c(5.5, 7, 1.5, 6, 4.5, 4.5, 5, 4, 1,  5.5, 2.5, 4.5, 2.5, 5.5, 4, 1, 4, 5, 2.5, 5.5), memory2 = c(5.5,  3, 1.5, 6, 4.5, 4.5, 5, 4, 5, 5.5, 6.5, 4.5, 2.5, 5.5, 4, 7,  8, 5, 6.5, 5.5), memory3 = c(5.5, 3, 4.5, 2, 4.5, 4.5, 5, 4,  5, 1.5, 6.5, 4.5, 6.5, 5.5, 4, 7, 4, 5, 6.5, 5.5), memory4 = c(1.5,  3, 4.5, 2, 1, 4.5, 5, 4, 5, 5.5, 2.5, 4.5, 2.5, 1.5, 4, 2, 4,  5, 2.5, 1.5), memory5 = c(5.5, 3, 4.5, 6, 4.5, 4.5, 5, 1, 5,  5.5, 6.5, 4.5, 6.5, 5.5, 4, 4, 4, 5, 2.5, 1.5), memory6 = c(5.5,  7, 7.5, 6, 8, 4.5, 5, 7.5, 5, 5.5, 6.5, 4.5, 6.5, 5.5, 4, 4,  4, 5, 2.5, 5.5), memory7 = c(1.5, 3, 4.5, 2, 4.5, 4.5, 1, 4,  5, 1.5, 2.5, 4.5, 6.5, 1.5, 4, 7, 4, 1, 6.5, 5.5), memory8 = c(5.5,  7, 7.5, 6, 4.5, 4.5, 5, 7.5, 5, 5.5, 2.5, 4.5, 2.5, 5.5, 8, 4,  4, 5, 6.5, 5.5)), .names = c("memory1", "memory2", "memory3",  "memory4", "memory5", "memory6", "memory7", "memory8"), row.names = c(492l,  509l, 510l, 518l, 519l, 522l, 527l, 533l, 535l, 542l, 543l, 557l,  558l, 560l, 567l, 569l, 578l, 581l, 582l, 584l), class = "data.frame") 

please ignore first unnamed column not relevant here.

> d     memory1 memory2 memory3 memory4 memory5 memory6 memory7 memory8 492     5.5     5.5     5.5     1.5     5.5     5.5     1.5     5.5 509     7.0     3.0     3.0     3.0     3.0     7.0     3.0     7.0 510     1.5     1.5     4.5     4.5     4.5     7.5     4.5     7.5 518     6.0     6.0     2.0     2.0     6.0     6.0     2.0     6.0 519     4.5     4.5     4.5     1.0     4.5     8.0     4.5     4.5 522     4.5     4.5     4.5     4.5     4.5     4.5     4.5     4.5 527     5.0     5.0     5.0     5.0     5.0     5.0     1.0     5.0 533     4.0     4.0     4.0     4.0     1.0     7.5     4.0     7.5 535     1.0     5.0     5.0     5.0     5.0     5.0     5.0     5.0 542     5.5     5.5     1.5     5.5     5.5     5.5     1.5     5.5 543     2.5     6.5     6.5     2.5     6.5     6.5     2.5     2.5 557     4.5     4.5     4.5     4.5     4.5     4.5     4.5     4.5 558     2.5     2.5     6.5     2.5     6.5     6.5     6.5     2.5 560     5.5     5.5     5.5     1.5     5.5     5.5     1.5     5.5 567     4.0     4.0     4.0     4.0     4.0     4.0     4.0     8.0 569     1.0     7.0     7.0     2.0     4.0     4.0     7.0     4.0 578     4.0     8.0     4.0     4.0     4.0     4.0     4.0     4.0 581     5.0     5.0     5.0     5.0     5.0     5.0     1.0     5.0 582     2.5     6.5     6.5     2.5     2.5     2.5     6.5     6.5 584     5.5     5.5     5.5     1.5     1.5     5.5     5.5     5.5 

if tabulate values memory1, following:

> table(d$memory1)    1 1.5 2.5   4 4.5   5 5.5   6   7    2   1   3   3   3   2   4   1   1  

so can see 5.5 mode here, , tried as.numeric(names(table(d$memory1))[which.max(table(d$memory1))]) did return 5.5. pretty clunky , how can iterate on 8 columns of data.frame? want resulting vector containing 8 modes (one corresponding each column). what's elegant way this?

this post provides elegant function determine mode need apply data frame.

mode <- function(x) {   ux <- unique(x)   ux[which.max(tabulate(match(x, ux)))] }  apply(d, 2, mode) 

yields:

memory1 memory2 memory3 memory4 memory5 memory6 memory7 memory8      5.5     5.5     4.5     1.5     4.5     5.5     4.5     5.5  

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -