[R] array problem and for looping

Huntsinger, Reid reid_huntsinger at merck.com
Thu Oct 28 22:18:35 CEST 2004

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

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: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Kunal Shetty
Sent: Thursday, October 28, 2004 1:55 PM
To: r-help at stat.math.ethz.ch
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



More information about the R-help mailing list