[R] matrices call a function element-wise
Jonathan P Daily
jdaily at usgs.gov
Mon Jan 3 20:09:15 CET 2011
IIRC, R is perfectly able to call matrices as vectors, so you might be
able to do this:
FT <- function(i) fisher.test(matrix(c(A[i],B[i],C[i],D[i]),2))
E <- sapply(1:1000000, FT)
Though I don't know how much time you will save.
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
the thing itself have purpose? Or do we, what's the word... imbue it."
- Jubal Early, Firefly
r-help-bounces at r-project.org wrote on 01/03/2011 09:57:14 AM:
> [image removed]
>
> [R] matrices call a function element-wise
>
> zhaoxing731
>
> to:
>
> R-help
>
> 01/03/2011 01:46 PM
>
> Sent by:
>
> r-help-bounces at r-project.org
>
> Hello
>
> I have 4 1000*1000 matrix A,B,C,D. I want to use the corresponding
> element of the 4 matrices. Using the "for loop" as follow:
>
> E<-o
> for (i in 1:1000)
> {for (j in 1:1000)
> {
> E<-fisher.test(matrix(c(A[i][j],B[i][j],C[i][j],D[i][j]),
> 2))#call fisher.test for every element
> }
> }
>
> It is so time-consuming
> Need vectorization
>
> Yours sincerely
>
>
>
>
> ZhaoXing
> Department of Health Statistics
> West China School of Public Health
> Sichuan University
> No.17 Section 3, South Renmin Road
> Chengdu, Sichuan 610041
> P.R.China
>
> [[alternative HTML version deleted]]
>
>
> __________________________________________________
> 8O?lW"2aQE;"3,4sH]A?Cb7QSJOd?
>
> ______________________________________________
> 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.
More information about the R-help
mailing list