[R] prblem with NA
Petr PIKAL
petr.pikal at precheza.cz
Tue Jan 6 09:00:46 CET 2009
Hi
r-help-bounces at r-project.org napsal dne 06.01.2009 02:29:18:
> 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)
Another one
X <-(da<=100)*1
Logical vectors can be treated like numeric with TRUE=1 and FALSE=0, I
learned that somewhere but it is not mentioned in help page for ?logical
so it is not so easy to find.
Regards
Petr
>
> And as 'data' is a function in the utils package include with R, you
> might consider not naming your variables 'data'.
>
> ______________________________________________
> 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