[R] Help transfrom R to C
Thomas Lumley
tlumley at u.washington.edu
Mon Apr 5 19:09:36 CEST 2010
On Mon, 5 Apr 2010, pinusan wrote:
>
> Dear R users,
>
> I would like to transform the following "for loop" from R-code to C-code
> because it takes really long time to have inc.freqy table.
> Unfortunately, I do not have experience to write C code.
> Plese, give me some example or advise to transfrom the R to C code.
I think you can just use
rowSums( test, unit.dist, "<=")
instead of your for() loop.
-thomas
> I have attached the code and some result that I made in R.
>
> Have a nice day.
>
> Hong Su
>
> # Data import
> ct10_pt1_neartree<-read.table("~/Desktop/hongsu/clustered_pattern/ct10/ct10/ct10_58ft_pt1_neartree",
> header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
> test<-round(ct10_pt1_neartree, digits=1)
>
> # randomly select 2 data set for test
> test<-as.matrix(test[sample(2,replace=T),])
> test
>
> # Selected Data
>> test
> Rpt1 Rpt2 Rpt3 Rpt4 Rpt5 Rpt6 Rpt7 Rpt8 Rpt9 Rpt10 Rpt11 Rpt12 Rpt13
> Trial1 19.3 9.1 20.7 3.0 21.4 14.5 8.2 10.5 7.4 11.0 6.9 9.5 15.4
> Trial2 24.5 22.2 4.9 7.8 20.9 12.5 18.3 6.5 7.6 2.2 8.9 19.6 21.1
> Rpt14 Rpt15 Rpt16 Rpt17 Rpt18 Rpt19 Rpt20 Rpt21 Rpt22 Rpt23 Rpt24
> Rpt25
> Trial1 49.2 39.9 18.3 14.8 29.9 27.3 20.2 14.6 9.5 14.2 7.5
> 16.0
> Trial2 38.6 12.8 1.5 12.5 4.6 10.6 14.5 12.5 5.8 14.0 55.9
> 4.8
> Rpt26 Rpt27 Rpt28 Rpt29 Rpt30 Rpt31 Rpt32
> Trial1 19.7 12.9 5.3 3.1 11.7 19.0 21.2
> Trial2 0.8 11.3 4.7 12.5 5.5 5.9 5.2
>
> # N of columns in test
> n.center.point<-ncol(test)
>
> # Maximum number in test
> max.dist<-max(test)
>
> # make distance
> unit.dist <- seq(0, round(max.dist, digit=1),0.1) # change file
>
>
> # for loop (need to change C)
> inc.freqy<-matrix(0,nrow(test), length(unit.dist))
>
> for(i in 1:nrow(test)){
> for (j in 1:length(unit.dist)){
> inc.freqy[i,j]<-length(test[i,][test[i,]<=unit.dist[j]])
> }
> }
>
> inc.freqy[,1:30]
>
> # partial result for inc.freqy
>> inc.freqy[,1:30]
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
> [,14]
> [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0
> 0
> [2,] 0 0 0 0 0 0 0 0 1 1 1 1 1
> 1
> [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
> [1,] 0 0 0 0 0 0 0 0 0 0 0 0
> [2,] 1 2 2 2 2 2 2 2 3 3 3 3
> [,27] [,28] [,29] [,30]
> [1,] 0 0 0 0
> [2,] 3 3 3 3
>
> --
> View this message in context: http://n4.nabble.com/Help-transfrom-R-to-C-tp1751764p1751764.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
More information about the R-help
mailing list