[R] finding the most frequent row
Uwe Ligges
ligges at statistik.uni-dortmund.de
Fri Dec 10 15:18:05 CET 2004
bogdan romocea wrote:
> Here's something that works. I'm sure there are better solutions (in
> particular the paste part - I couldn't figure out how to avoid typing
> a[i,1], ..., a[i,10]).
>
> a <- matrix(nrow=1000,ncol=10)
> for (i in 1:1000)
> for (j in 1:10)
> a[i,j] <- sample(1:0,1)
>
> b <- vector(mode="character")
> for (i in 1:1000)
> b[i] <- paste(a[i,1],a[i,2],a[i,3],a[i,4],a[i,5],
> a[i,6],a[i,7],a[i,8],a[i,9],a[i,10],sep="")
>
> #the most frequent row
> table(b)[table(b) == max(table(b))]
You mean:
a <- matrix(sample(0:1, 10000, replace = TRUE), 1000, 10)
b <- apply(a, 1, paste, collapse="")
tb <- table(b)
tb[which.max(tb)]
The real problem is that paste() is slow compared to numerical
calculations. I think there is a better solution somewhere in the
archives (I cannot remind right now), so look up the archives, please.
Uwe Ligges
> HTH,
> b.
>
>
> -----Original Message-----
> From: Lisa Pappas [mailto:lisa.pappas at hci.utah.edu]
> Sent: Thursday, December 09, 2004 5:15 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] finding the most frequent row
>
>
> I am bootstrapping using a function that I have defined. The
> "Statistic" of the function is an array of 10 numbers. Therefore if
> I use 1000 replications, the "t" matrix will have 1000 rows each of
> which is a bootstrap replicate of this 10 number array (10 columns).
> Is there any easy way in R to determine which row appears the most
> frequently?
>
> Thanks,
> Lisa Pappas
>
> Huntsman Cancer Institute wishes to promote open communication while
> protecting confidential and/or privileged information. If you have
> received this message in error, please inform the sender and delete
> all copies.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list