Hi how can I find, in a vector of characters, which is the most frequent one? Thanks Gabriele Zoppoli, MD Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University of Genova, Genova, Italy Guest Researcher, LMP, NCI, NIH, Bethesda MD Work: 301-451-8575 Mobile: 301-204-5642 Email: zoppolig at mail.nih.gov
Hello,
What about
test <- c(rep(c("A","C","G","T"),
20),"C")
table(test)
I am not sure that is an apt representation of the data and problem
you are trying to solve. It also occurred to me that each character
may not be its own element in the vector. If that is so, you would
need to do something a little differently.
Let us know.
Josh
On Thu, Apr 22, 2010 at 10:36 AM, Zoppoli, Gabriele (NIH/NCI) [G]
<zoppolig at mail.nih.gov> wrote:> Hi
>
> how can I find, in a vector of characters, which is the most frequent one?
>
> Thanks
>
> Gabriele Zoppoli, MD
> Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University
of Genova, Genova, Italy
> Guest Researcher, LMP, NCI, NIH, Bethesda MD
>
> Work: 301-451-8575
> Mobile: 301-204-5642
> Email: zoppolig at mail.nih.gov
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/
Hi Gabriele,
This is one way but I'm sure that there is an optimal way of doing so...
x <-
c("A","B","C","C","C","C","C","B","B")
name <- unique(x) # get unique characters
freq <- c()
for (a in 1:length(name)){
freq[a] <- sum(x==name[a])} # get frequency
out <- cbind(name,freq)
Muhammad
Zoppoli, Gabriele (NIH/NCI) [G] wrote:> Hi
>
> how can I find, in a vector of characters, which is the most frequent one?
>
> Thanks
>
> Gabriele Zoppoli, MD
> Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University
of Genova, Genova, Italy
> Guest Researcher, LMP, NCI, NIH, Bethesda MD
>
> Work: 301-451-8575
> Mobile: 301-204-5642
> Email: zoppolig at mail.nih.gov
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
On Apr 22, 2010, at 1:36 PM, Zoppoli, Gabriele (NIH/NCI) [G] wrote:> Hi > > how can I find, in a vector of characters, which is the most > frequent one?> a <- sample(letters[1:10], 100, replace=TRUE) > table(a) a a b c d e f g h i j 10 7 7 13 7 12 1 14 15 14 > max(table(a)) [1] 15 > which.max(table(a)) i 9 # so the ninth element in the table is the max > table(a)[which.max(table(a))] i 15 # but the table entry needs to be extracted if you want the count.> > Thanks > > Gabriele Zoppoli, MD > Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, > University of Genova, Genova, Italy > Guest Researcher, LMP, NCI, NIH, Bethesda MD > > Work: 301-451-8575 > Mobile: 301-204-5642 > Email: zoppolig at mail.nih.gov > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Gabriele - How about this:> data<-c("A","A","A","B","B","C","D") > data[1] "A" "A" "A" "B" "B" "C" "D"> table(data)data A B C D 3 2 1 1 Thanks, Lee ". . . people who focus only on 'what is' will create more of 'what is.' People who focus mostly on 'what could be' will begin to create 'what could be.'" ~ Laura Beth Jones Lee P. Adams | Fuel & Emissions Strategy | Luminant Energy | lee.adams at luminant.com | 214.875.8737 Office | 214.701.0687 Mobile -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Zoppoli, Gabriele (NIH/NCI) [G] Sent: Thursday, April 22, 2010 12:37 PM To: r-help at r-project.org Subject: [R] simple question Hi how can I find, in a vector of characters, which is the most frequent one? Thanks Gabriele Zoppoli, MD Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University of Genova, Genova, Italy Guest Researcher, LMP, NCI, NIH, Bethesda MD Work: 301-451-8575 Mobile: 301-204-5642 Email: zoppolig at mail.nih.gov ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Confidentiality Notice: This email message, including an...{{dropped:10}}
Or use this handy-dandy function:
smode<-function(x){
xtab<-table(x)
modes<-xtab[max(xtab)==xtab]
mag<-as.numeric(modes[1]) #in case mult. modes, this is safer
themodes<-names(modes)
mout<-list(themodes=themodes,modeval=mag)
return(mout)
}