[R] R help
arun
smartpink111 at yahoo.com
Fri Feb 7 17:12:23 CET 2014
Hi,
If there are multiple max values, you will get only the first value with ?which.max().
Using dataset `a` as an example:
a <- structure(list(`1` = c("Mal", "Mahesh", "Tarak", "Pawan", "Charan",
"Prabhas", "Arjun", "Naresh", "Nithin", "Puri"), `2` = 1:10,
`3` = c("Layer", "Actor", "Actor", "Actor", "Actor", "Actor",
"Actor", "Actor", "Actor", "Director"), `4` = c(22L, 45L,
30L, 47L, 32L, 32L, 31L, 29L, 30L, 38L), `5` = c("M", "M",
"M", "M", "M", "M", "M", "M", "M", "M"), `6` = c(100000L,
150000000L, 150000000L, 150000000L, 100000000L, 100000000L,
90000000L, 30000000L, 30000000L, 100000000L)), .Names = c("1",
"2", "3", "4", "5", "6"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"))
sapply(a,function(x) which.max(table(factor(x,levels=unique(x)))))
# 1.Mal 2.1 3.Actor 4.30 5.M 6.150000000
# 1 1 2 3 1 2
#or
Mode <- function(x) {
ux <- unique(x)
which.max(tabulate(match(x,ux)))}
sapply(a,Mode)
#1 2 3 4 5 6
#1 1 2 3 1 2
#If you want the index of all the max values:
lapply(a,function(x) {h <- table(factor(x,levels=unique(x))); which(h%in% max(h))})
A.K.
On Thursday, February 6, 2014 4:02 AM, Malyadri Putchakayala <malyadri.putchakayala at nuevora.com> wrote:
How to find Mode?
h<-table(q$GENDER)
h<-which.max(h)
I am using above code. But I have 10000 Variable Names of Data, Then How to
find (above is find at a time only one mode, I need at a time complete
dataset mode.)
Eg:1000 variable Names,500 Character names,500 Numeric Names
like(Gender,salary,occupation,age......) then how to find mode.
More information about the R-help
mailing list