Message-ID: <4962B3EE.2090307@biostat.wisc.edu>
Date: 2009-01-06T01:29:18Z
From: Erik Iverson
Subject: prblem with NA
In-Reply-To: <21301527.post@talk.nabble.com>
Hello -
kayj wrote:
>
> Hi all
>
> I have a data set with the total number of columns =ncol, and the total
> number of rows=nrow. I am trying to loop over the values and id the value is
> less than or equal to 100 to be changed to 1. if the value is greater than
> 100 , to be changed to 0. if NA , let X[i,j]=NA. I ran into a problem where
> if one row in my data set had all values =NA, then the program does not
> continue working past that row!
>
> At some point I get the following error message:
>
> ?Error in if (data [i, j] <= 100) { : missing value where TRUE/FALSE needed?
>
> Here is the program
>
> data<-read.table("fileName.txt", header=F, sep='\t')
>
> X=data
> for(i in ncol)
> {
> for(j in nrow)
> {
> if(data[i,j]<=100) {X[i,j]=1}
> if(data[i,j]>100) {X[i,j]=0}
>
> if(is.na(data[i,j])) {X[i,j]=NA}
> }
> }
An alternate, vectorized solution may be:
X <- ifelse(data <= 100, 1, 0)
And as 'data' is a function in the utils package include with R, you
might consider not naming your variables 'data'.