[R] Summarizing levels for future commands

Douglas Bates bates at stat.wisc.edu
Wed Apr 16 04:57:35 CEST 2003


SuzieBlatt at netscape.net (Suzanne E. Blatt) writes:

> Hi.  This will hopefully be readily understood but if not, bear with me.
> 
> I have to do a repeated analysis (in spatstat) and want to batch file it.  For each of my 'runs' certain variables change.  At present I am manually specifying these changes and want to automate it if possible.
> 
> Ok, I am creating an object which is comprised of 'levels' that are 'characters'.  Further in my program I need to select one of these 'levels' as the comparison to the others.  The one I want to select is the most frequent and then compare it to the second most frequent.  Is there anyway to get R to determine the most frequency of 'levels' in an object and then use a specific one in future functions?  I couldn't find it in my search through the manual or the r-help archives.
> 
> I hope what I am attempting to do is clear, let me know if it isn't.

I think I know what you want to do but I'm not sure.  I believe you
want to find the mode, or the "most popular" level.  For example, in
the following sample of size 50 from the values 1:10

> samp = sample(1:10, 50, replace = TRUE)
> table(samp)
samp
 1  2  3  4  5  6  7  8  9 10 
 5  3  7  3  4  2  8  9  3  6 

the most popular value is 8.

As you can see, the table function tells you the frequencies of the
values.  From that it is just a matter of extracting the index of the
value with the maximum count and getting the label.

> names(tbl)[match(max(tbl), tbl)]
[1] "8"

Hope this helps.



More information about the R-help mailing list