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. Thanks, Suzanne
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.
Hello,
Thanks to those who have been responding to my query.
I've done the following, and get the following results:
table(x)
"elm.american" "hawthorn" "ironwood"
2 1 4
which.max(table(x))
"3"
The first 'table' is fine, the 'which.max' part not. Can I get
it to actually use "ironwood", or is numeric all I can hope for? I
tried putting 'as.character' in front of the 'which.max' command
but that didn't work. The 'names(tbl)[match(max(tbl), tbl)]' also
didn't work for me, claims it didn't know what the 'names'
function was.
Any further suggestions most welcome,
Suzanne
>> 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.
>
Hello again, Figured out one of my errors, forgot the 's' on the 'names' command. So, now I can get my max (or min) of the levels within my table and I can plug them successfully into my function farther along my code. However, how can I get the 2nd or 3rd most frequent level and use them as well as the most frequent level? Thanks for the continued help, Suzanne Douglas Bates <bates at stat.wisc.edu> wrote:>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. >
Let's say
x <- c("eml.american", "hawthorn", "ironwood")
x <- rep(x, c(2, 1, 4))
y <- table(x)
names(y)[match(max(y), y)]
SuzieBlatt@netscape.net (Suzanne E. Blatt)
Sent by: r-help-bounces@stat.math.ethz.ch
04/16/2003 07:17 AM
To: bates@stat.wisc.edu (Douglas Bates)
cc: r-help@r-project.org
Subject: Re: [R] Summarizing levels for future commands
Hello,
Thanks to those who have been responding to my query.
I've done the following, and get the following results:
table(x)
"elm.american" "hawthorn" "ironwood"
2 1 4
which.max(table(x))
"3"
The first 'table' is fine, the 'which.max' part not. Can I get
it to
actually use "ironwood", or is numeric all I can hope for? I tried
putting 'as.character' in front of the 'which.max' command but
that didn't
work. The 'names(tbl)[match(max(tbl), tbl)]' also didn't work for
me,
claims it didn't know what the 'names' function was.
Any further suggestions most welcome,
Suzanne
>> 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.
>
______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
[[alternate HTML version deleted]]
Thank you all! I got my program doing what I want it to - it will save me ALOT of time!! Suzanne Douglas Bates <bates at stat.wisc.edu> wrote:>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. >