s-news
[Top] [All Lists]

dimnames in Sparc Splus 6.0

To: s-news@lists.biostat.wustl.edu
Subject: dimnames in Sparc Splus 6.0
From: Gary Sabot <gary@sabot.com>
Date: Wed, 5 Sep 2001 12:48:37 -0400
In-reply-to: <15253.31719.88959.727681@delphioutpost.com>
References: <15253.31719.88959.727681@delphioutpost.com>
   To: s-news@lists.biostat.wustl.edu
   From: Gary Sabot <gary@sabot.com>
   Date: Tue, 4 Sep 2001 21:12:07 -0400
   Subject: [S] dimnames in Sparc Splus 6.0
   
   
   I'm having a problem involving the behavior of dimnames in the Sparc
   Splus 6.0 relative to earlier releases...

Terry Therneau <therneau@mayo.edu> supplied the fix that I needed.  It
works fine on Sparc Splus6.0.

A copy of the fix is below, with some additional comments and tests:

#first make sure that if you run this twice, you still get the real original
data.frame.original.fcn <- get("[.data.frame", where="splus")

#make sure that dataframe[,col.id] keeps its dimnames
"[.data.frame" <- function(x,..., drop=T) 
{
  result <- data.frame.original.fcn(x,..., drop=F)
  if (drop && ncol(result)==1) {
    #new style creation function, convenient way to create named vector
    new('named', .Data=as.vector(unlist(result)), 
        .Names=dimnames(result)[[1]])
  } else {
    if (!missing(drop) && drop && nrow(result)==1) {
      #replicate documented behavior of [.data.frame: drop=T acts
      #differently then missing drop arg for this case!
      as.list(result)
    } else {
      result
    }
  }
}

#A few test cases:
#    > data.frame(cbind(col1=c(a=1,b=2),col2=c(c=3,d=4)))[,2]
#     c d 
#     3 4
#    > data.frame(cbind(col1=c(a=1,b=2),col2=c(c=3,d=4)))[2,]
#      col1 col2 
#    d    2    4
#    > data.frame(cbind(col1=c(a=1,b=2,c=3),col2=c(d=4,e=5,f=6)))[c(1,2),2]
#     d e 
#     4 5
#    > data.frame(cbind(col1=c(a=1,b=2,c=3),col2=c(d=4,e=5,f=6)))[c(3,2,1),2]
#     f e d 
#     6 5 4
#    > data.frame(cbind(col1=c(a=1,b=2),col2=c(c=3,d=4)))[2,,drop=T]
#    $col1:
#    [1] 2
#    
#    $col2:
#    [1] 4
#    
#    > data.frame(cbind(col1=c(a=1,b=2),col2=c(c=3,d=4)))[2,,drop=F]
#      col1 col2 
#    d    2    4
#    > 

<Prev in Thread] Current Thread [Next in Thread>