#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
> -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&q
"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
_
Thanks Bill and David!
John
- Original Message -
From: William Dunlap
To: array chip ; "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 warning that it was
written for matrices and c
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);
---Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
> Behalf Of array chip
> Sent: Monday, August 29, 2011 5:03 PM
> To: r-help@r-project.org
> Subject: [R] weird apply() behavior
>
> Hi, I had a weird results from using apply(). H
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 r
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 ou
8 matches
Mail list logo