Hi, I had a weird results from using apply(). Here is an simple example:
> y<-data.frame(list(a=c(1,NA),b=c('2k','0')))
> y
a b
1 1 2k
2 NA 0
> apply(y,1,function(x){x<-unlist(x); if (!is.na(x[2]) & x[2]=='2k' &
> !is.na(x[1]) & x[1]=='1') 1 else 0} )
This should print "1 0" as output, as demonstrated by:
> apply(y[1,],1,function(x){x<-unlist(x); if (!is.na(x[2]) & x[2]=='2k' &
> !is.na(x[1]) & x[1]=='1') 1 else 0} )
1
1
> apply(y[2,],1,function(x){x<-unlist(x); if (!is.na(x[2]) & x[2]=='2k' &
> !is.na(x[1]) & x[1]=='1') 1 else 0} )
2
0
But it actually prints:
> apply(y,1,function(x){x<-unlist(x); if (!is.na(x[2]) & x[2]=='2k' &
> !is.na(x[1]) & x[1]=='1') 1 else 0} )
[1] 0 0
Anyone has any suggestion?
Thanks
John
______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.