# [R] hi, i have a problem in R

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Aug 6 05:53:11 CEST 2009

```Hi Gina,

On Aug 5, 2009, at 11:25 PM, Gina Liao wrote:

> Hi,
> I'm sorry i didn't say clearly.
>
>
>
>> for (i in 1:10){
>
> +     print(sample(9,4,replace=T))
>
> + }
> [1] 2 5 5 2
> [1] 6 2 1 5
>
> [1] 9 5 9 7
> [1] 2 6 4 1
> [1] 8 5 4 5
> [1] 6 2 3 7
>
> [1] 6 1 7 3
>
> [1] 9 5 4 7
>
> [1] 6 4 8 5
> [1] 1 5 6 3
>
> I mean when it shows these reults.
> Then, what should I do to show the top 3 numbers with highest
> frequencies for each position.
> It shows ten rows and four lists. But I'd like to calculate the
> highest frequencies in each list.
> For example, in the first list, the highest frequencie is 6.
> Because I have to do that procedure for 100 times, and it's possible
> to calculate by self.

You can't do anything with the data when you're just printing -- store
your numbers in a matrix, and play with the table/max/which.max
functions. If I understand you correctly, you want to find which
number appears the most in each column of your data. If so, this code
should get you close to where you need to be -- use your favorite way
to iterate over the columns of you data.

R> dat <- matrix(sample(1:9, 30, rep=T), 10)
R> dat
[,1] [,2] [,3]
[1,]    1    7    1
[2,]    6    5    2
[3,]    9    6    4
[4,]    5    2    5
[5,]    9    3    1
[6,]    4    7    7
[7,]    5    6    4
[8,]    2    6    5
[9,]    5    1    9
[10,]    7    1    4

R> counts <- table(dat[,1])
R> counts
1 2 4 5 6 7 9
1 1 1 3 1 1 2

R> which.max(counts)
5
4

R> names(which.max(counts))
[1] "5"

R> as.numeric(names(which.max(counts)))
[1] 5

I think that should help,
-steve

--
Steve Lianoglou