Skip to content

Numeric class and sasxport.get

6 messages · Sebastien Bihorel, Frank E Harrell Jr

#
Thanks a lot Frank,

One last question, though. I was tempted to remove all attributes of my 
variables after the sasxport.get call using
foo <- sasxport.get(...)
foo <- as.data.frame(lapply(unclass(foo),as.vector))
Since I never worked with the objects of class 'labeled', I was 
wondering what I will loose by removing this attribute.

*Sebastien Bihorel, PharmD, PhD*
PKPD Scientist
Cognigen Corp
Email: sebastien.bihorel at cognigencorp.com 
<mailto:sebastien.bihorel at cognigencorp.com>
Phone: (716) 633-3463 ext. 323
Frank E Harrell Jr wrote:
#
Sebastien Bihorel wrote:
Not a good idea, for many reasons including dates and other types.

And the labelled type is need if you subset the data, in order to keep 
the labels.

Note that your original issue is related to "class" being "soft" for 
integers and regular numerics:

  x <- 1:3
 > attributes(x)
NULL
 > class(x)
[1] "integer"
 > x <- runif(3)
 > class(x)
[1] "numeric"
 > attributes(x)
NULL

Frank

  
    
#
I also realized the flaw after testing the script on various datasets...

Following up on your last note:
1- Is that the reason why the class of integer and regular numeric 
variable is solely "labelled" following sasxport.get?
2- Can class be 'soft' for other 'kind' of variables?
3- Would you anticipate the following wrapper function to generate 
incompatibilities with other R functions?


SASxpt.get <- function(file, force.single = TRUE,
                  method=c('read.xport','dataload','csv'), formats=NULL, 
allow=NULL,
                  out=NULL, keep=NULL, drop=NULL, as.is=0.5, FUN=NULL) {
 
  foo <- sasxport.get(file=file, force.single=force.single, method=method,
                      formats=formats, allow=allow, out=out, keep=keep,
                      drop=drop, as.is=as.is, FUN=FUN)
 
  # For each variable of class "labelled" (and only "labelled"), add the 
native class as a second class argument

  sglClassVarInd <- which(lapply(lapply(unclass(foo),class),length)==1)
 
  for (i in 1:length(sglClassVarInd)){
    x <- foo[,sglClassVarInd[i]]   
    if (class(x)=="labelled") class(foo[,sglClassVarInd[i]]) <- 
c(class(x), class(unclass(x)))
  }
  return(foo)
}


*Sebastien Bihorel, PharmD, PhD*
PKPD Scientist
Cognigen Corp
Email: sebastien.bihorel at cognigencorp.com 
<mailto:sebastien.bihorel at cognigencorp.com>
Phone: (716) 633-3463 ext. 323
Frank E Harrell Jr wrote:
#
Sebastien Bihorel wrote:
Yes.  R gurus might correct me but just creating a numeric vector 
doesn't create a 'hard' class, add adding your own class attribute equal 
to 'numeric' or 'integer' might cause a problem downstream.
Not that I can recall.
I'm going to beg off on that.  I'm not enough of an expert on the impact 
of adding such classes.

Frank

  
    
#
Ok, just so as I get that straight, is the 'labelled' class something 
that you created in your package or a readily available class in base R?

*Sebastien Bihorel, PharmD, PhD*
PKPD Scientist
Cognigen Corp
Email: sebastien.bihorel at cognigencorp.com 
<mailto:sebastien.bihorel at cognigencorp.com>
Phone: (716) 633-3463 ext. 323
Frank E Harrell Jr wrote:
#
Sebastien Bihorel wrote:
It's something we added for the Hmisc package.
Signing off,
Frank