[R] weird apply() behavior

William Dunlap wdunlap at tibco.com
Tue Aug 30 02:21:59 CEST 2011


apply() should come with a big warning that it was
written for matrices and can cause a lot of surprises
with data.frames.  apply(X, ...) converts X to a
matrix and that is the kernel of your problem: if any
columns of X are not numeric then as.matrix(X) is
a character matrix whose columns are made with calls
to format(), which means that all entries in the column
have the same number of characters in them.   This is
handy for printing but not for other purposes.

  > as.matrix(y)
       a    b
  [1,] " 1" "2k"
  [2,] NA   "0"
  > as.matrix(y[1,])
    a   b
  1 "1" "2k"
  > as.matrix(y[2,])
    a  b
  2 NA "0"

Another way to debug this on your own is to simplify the
function you are giving to apply.  Then you would see
where things are going wrong (but perhaps not why):

  > apply(y, 1, function(x)unlist(x))
    [,1] [,2]
  a " 1" NA
  b "2k" "0"
  > apply(y[1,], 1, function(x)unlist(x))
    1
  a "1"
  b "2k"
  > apply(y[2,], 1, function(x)unlist(x))
    2
  a NA
  b "0"


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of array chip
> Sent: Monday, August 29, 2011 5:03 PM
> To: r-help at r-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[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
> 
> 
> ______________________________________________
> 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