Re: [R] array problem and for looping
Hi Beter not to give a same name your values (variables) as is function name. R is quite clever and sample <- rnorm(10) sample <- sample(sample,3) works as expected, but it is not a rule. Cheers Petr On 28 Oct 2004 at 17:54, Kunal Shetty wrote: > Dear R- users and Helpers > > Is there some way to re initialise or clear the array elements? > Pardon me for being vague but the problem itself quite vague. I have > attached the code along with email. When I run the saved r- code using > source("random1.txt") , command. > > The program runs fine..but at times there is an error see below # ; > but again after the error if re-excuted it would work fine?I probably > missed some array detail in my program any suggestions > > #Error in var(parrX[1, ]) : missing observations in cov/cor > > parrX[] is an array . > > 2) Also pardon me for the lengthy procedural code > but as you could see in it..i have used the for loop for > finding the positions (indexes) of the missing values and > later carry out updating the new array element values at those > particular positions/ > So how can I escape the for loop in this case ? i.e get the > missing position indexes and save another object ay vector or > array ? > > And also later wanted to use matrix or vector multiplication > (%*%) for the updating statement > newy[i]<- u2 + covXY/varX * (sample$x[i] - u1) > > is any of the apply function good out here ? > > I really feel that I am doing something very routine and donkey work > and I am most certain that powerful R ? functions could just execute > the same 10 liner for loop condition to mere 4 lines ? but how?I am > getting lost in the sea of functions here? > > Thank u for reading > Regards > Kunal > Petr Pikal [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] array problem and for looping
First, the condition if (is.na(sample$x[i]== TRUE)) asks if sample$x[i] is equal to TRUE, and then checks whether the result of this comparison is NA. Because the comparison returns NA when one or the other argument is NA, this works, but note that it would work as well with FALSE in place of TRUE. I suppose you meant to say if (is.na(sample$x[i]) == TRUE) which is the same as if(is.na(sample$x[i])). Second, your code could generate data with *both* x and y missing simultaneously. That would produce missing results in your regression imputation. You probably want to check for these and drop them or otherwise handle them. Third, you might gain some insight by running your function algoResult on known data, and examining the results. The missing values have to come from somewhere, but because you run the entire script which generates random data each time you never get this check. Fourth, you function doesn't use its last 3 arguments, but does use the dataframe "sample" and the vector "missing". As these are not passed as arguments, they are searched for in the enclosing environment. Do you want that? Fifth, yes, you don't need the loop. Just use subscripting for example, as you did to insert NAs randomly, or ifelse as you used to impute by means. Reid Huntsinger -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kunal Shetty Sent: Thursday, October 28, 2004 1:55 PM To: [EMAIL PROTECTED] Subject: [R] array problem and for looping Dear R- users and Helpers Is there some way to re initialise or clear the array elements? Pardon me for being vague but the problem itself quite vague. I have attached the code along with email. When I run the saved r- code using source("random1.txt") , command. The program runs fine..but at times there is an error see below # ; but again after the error if re-excuted it would work fine...I probably missed some array detail in my program any suggestions #Error in var(parrX[1, ]) : missing observations in cov/cor parrX[] is an array . 2) Also pardon me for the lengthy procedural code but as you could see in it..i have used the for loop for finding the positions (indexes) of the missing values and later carry out updating the new array element values at those particular positions/ So how can I escape the for loop in this case ? i.e get the missing position indexes and save another object ay vector or array ? And also later wanted to use matrix or vector multiplication (%*%) for the updating statement newy[i]<- u2 + covXY/varX * (sample$x[i] - u1) is any of the apply function good out here ? I really feel that I am doing something very routine and donkey work and I am most certain that powerful R - functions could just execute the same 10 liner for loop condition to mere 4 lines ? but how...I am getting lost in the sea of functions here... Thank u for reading Regards Kunal __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] array problem
Replace your line that updates A with this: p <- unique(c(i,j,1:5)) f <- function(x) diag( matrix(x,4,4) ) AA <- apply( outer(A,G/B), p[-(1:2)], f ) # form product A <- aperm( array( AA, dim(A) ), order(p) ) # reshape Here are a couple of tests. You might want to do some more tests yourself as well since these are the only ones I did: > > i <- 3; j <- 5 > G <- 100 * matrix(1:4,2) > B <- 1+0*G # all ones > A <- array(1:32,c(2,2,2,2,2)) > A2 <- A > A2[,,1,,1] <- A2[,,1,,1] * G[1,1] > A2[,,1,,2] <- A2[,,1,,2] * G[1,2] > A2[,,2,,1] <- A2[,,2,,1] * G[2,1] > A2[,,2,,2] <- A2[,,2,,2] * G[2,2] > > test <- function(A,i,j) { + p <- unique(c(i,j,1:5)) + f <- function(x) diag( matrix(x,4,4) ) + AA <- apply( outer(A,G/B), p[-(1:2)], f ) # form product + A <- aperm( array( AA, dim(A) ), order(p) ) # reshape + A + } > > identical(test(A,i,j),A2) [1] TRUE > > > i <- 2; j <- 4 > A3 <- A > A3[,1,,1,] <- A3[,1,,1,] * G[1,1] > A3[,1,,2,] <- A3[,1,,2,] * G[1,2] > A3[,2,,1,] <- A3[,2,,1,] * G[2,1] > A3[,2,,2,] <- A3[,2,,2,] * G[2,2] > > identical(test(A,i,j),A3) [1] TRUE > --- Date: Sun, 04 Jan 2004 21:42:55 +0800 From: Z P <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Subject: [R] array problem Dear all, I define , for n=5 or any integer greater than 0. A<-array((1/2)^n , c(rep(2,n))) then for any i not equal to j, and 1<=i,j<=n, B<-apply(a,c(i,j),sum) now B is a 2 by 2 matrix, I also define another costant 2 by 2 matrix G, How can I change the values of each elements of array A, according the rule that, for example, i=3,j=5, A[i1,i2,m,i4,l]<-A[i1,i2,m,i4,l]*G[m,l]/B[m,l] , where m,l=1,2 and i1,i2,i4=1,2 I can control this given any i and j, however, I must do the iteration for i in 1:(n-1) { for j in (i,n) {B<-apply(a,c(i,j),sum) here change the value of every elements of A according to my rule } } Is there any easy way to change the value of A in the iteration? Thank you. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help