[R] Odp: How to repeat "for" function?
petr.pikal at precheza.cz
Mon Feb 1 16:56:58 CET 2010
r-help-bounces at r-project.org napsal dne 01.02.2010 14:51:17:
> Dear Users,
> I have one problem here, I tried many time and even read a few notes on
> writing function but still.....
> Can anyone help me on how to simplify Part B (please refer the
> below), so that I don't have to repeat the calculation of "num" and "r"
> Thank you very much..every help is very much appreciated...
> ## Part A
> ## Part B
is same as
num1 <- 1:n
> for(i in 1:n)num1[i]=pc%*%X[i,1]
If I understand what the above code does I am a bit surprised
for each number in cycle
X[i,1] is again a number
so it basically results in
pc * X[,1]
which you can compute for all 4 columns by
kronecker(pc, X, "*")
But I wonder if this is what you really want.
Maybe you shall think it over again and try with some smaller manageable
smallX <- X[1:5,]
do all your computation and if it does not produce what you want, specify
what you want.
> for(i in 1:n)num2[i]=pc%*%X[i,2]
> for(i in 1:n)num3[i]=pc%*%X[i,3]
> for(i in 1:n)num4[i]=pc%*%X[i,4]
> for(i in 1:n)den[i]=pc%*%X[i,]
> MLAV=sum(r2>r1 & r2>r3 & r2>r4)
> Best regards,
> View this message in context:
> Sent from the R help mailing list archive at Nabble.com.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help