Re: [R] weird apply() behavior

2011-08-30 Thread Daniel Malter
#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

[R] weird apply() behavior

2011-08-29 Thread array chip
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

Re: [R] weird apply() behavior

2011-08-29 Thread David Winsemius
On Aug 29, 2011, at 8:02 PM, array chip wrote: 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); That is quite unnecessary since apply coerces the

Re: [R] weird apply() behavior

2011-08-29 Thread William Dunlap
-project.org Subject: [R] weird apply() behavior 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

Re: [R] weird apply() behavior

2011-08-29 Thread David Winsemius
On Aug 29, 2011, at 8:17 PM, David Winsemius wrote: On Aug 29, 2011, at 8:02 PM, array chip wrote: 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);

Re: [R] weird apply() behavior

2011-08-29 Thread array chip
Thanks Bill and David! John - Original Message - From: William Dunlap wdun...@tibco.com To: array chip arrayprof...@yahoo.com; r-help@r-project.org r-help@r-project.org Cc: Sent: Monday, August 29, 2011 5:21 PM Subject: RE: [R] weird apply() behavior apply() should come with a big

Re: [R] weird apply() behavior

2011-08-29 Thread Heikki Kaskelma
array chip: Hi, I had a weird results from using apply(). Consider: y-data.frame(list(a=c(1,NA), b=c('2k','0'))) do.call(args=y, Vectorize(v=c(a,b), function(a,b) if(is.na(z-b==2k a==1)) 0 else z+0)) Heikki Kaskelma Munkkiniemi

Re: [R] weird apply() behavior

2011-08-29 Thread William Dunlap
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Heikki Kaskelma Sent: Monday, August 29, 2011 6:13 PM To: r-h...@stat.math.ethz.ch Subject: Re: [R] weird apply() behavior array chip: Hi, I had a weird results from using