[R] weird apply() behavior

Daniel Malter daniel at umd.edu
Tue Aug 30 11:36:16 CEST 2011


#Do

apply(y,1,print)

#Note the space that is inserted before the "1." If you insert this space in
your function

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} )

#you get the result you expect.

#Also, note that your !is.na conditions are redundant for the given example
because if the other conditions are true, the !is.na conditions default to
true, as well.

apply(y,1,function(x){x<-unlist(x); if (x[2]=='2k' & x[1]==' 1') 1 else 0} )

HTH,
Daniel




--
View this message in context: http://r.789695.n4.nabble.com/weird-apply-behavior-tp3777699p3778318.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list