[R] Help with conversion of variable labels (Hmisc and Epicalc)

David Winsemius dwinsemius at comcast.net
Sat Sep 27 00:14:33 CEST 2014


On Sep 26, 2014, at 4:21 AM, Otto Pichlhöfer wrote:

> For my epidemiological analysis I use the packages Epicalc and Hmisc –
> among others. Both packages allow to assign variable labels that will
> appear in the output of the respective packages’ own functions. The  
> modes
> of storage of the labels in a dataframe are very different. I am
> wonderiung if there is a function that would allow to easily convert
> Epicalc labels to Hmisc labels and possibly the other way around.
>
> Unfortunately I am not adept enough in R to write such a function  
> myself.

Here's some further explorations on the topic. You should be able to  
see that epicalc (not "Epicalc") uses labeling at a dataframe levels  
of attribute assignment while rms/Hmisc uses a column-level attribute  
creation. So you could actually have both systems working in the same  
dataframe. (Which could get pretty confusing if your short-term memory  
is as limited as mine.)

require(epicalc)
require(rms) # which loads Hmisc
# This is the first section of the examples in
 > sbp <- c(120, 100, 110, 120, 140, 120,  NA,  NA)
 > dbp <- c( 80,  80,  70,  80,  70,  NA,  70,  60)
 > .data <- data.frame(sbp, dbp)
 > use(.data)

I'm guessing that this `use`-function is sort of like R's attach  
function
I strongly recommend against using `attach` and I suspect also against  
using `use`

 > pack()   # I have no idea what that does.
 > des()   # or that

  No. of observations =  8
   Variable      Class           Description
1 sbp           numeric
2 dbp           numeric
 > label.var(sbp, "systolic BP")
 > str(.data$sbp)
  num [1:8] 120 100 110 120 140 120 NA NA
 > str(.data)
'data.frame':	8 obs. of  2 variables:
  $ sbp: num  120 100 110 120 140 120 NA NA
  $ dbp: num  80 80 70 80 70 NA 70 60
  - attr(*, "var.labels")= chr  "systolic BP" ""

That shows that the attributes are assigned to the dataframe by  
epicalc's `var.labels`
 >
 > ?label
 > label(.data$sbp) <- "test.sbp"
 > str(.data)
'data.frame':	8 obs. of  2 variables:
  $ sbp:Classes 'labelled', 'numeric'  atomic [1:8] 120 100 110 120  
140 120 NA NA
   .. ..- attr(*, "label")= chr "test.sbp"
  $ dbp: num  80 80 70 80 70 NA 70 60
  - attr(*, "var.labels")= chr  "systolic BP" ""

And that shows that `Hmisc::label` does its assignment to the  
individual column vector.

If you wanted to assign Hmisc-type labels to the columns of a  
dataframe that has some or all of its columns lableded in the epicalc  
method then this loop succeeds:

 > for( coln in seq_along(.data) ) { label(.data[[coln]]) <-  
attr(.data, "var.labels")[coln]}
 > str(.data)
'data.frame':	8 obs. of  2 variables:
  $ sbp:Classes 'labelled', 'numeric'  atomic [1:8] 120 100 110 120  
140 120 NA NA
   .. ..- attr(*, "label")= chr "systolic BP"
  $ dbp:Classes 'labelled', 'numeric'  atomic [1:8] 80 80 70 80 70 NA  
70 60
   .. ..- attr(*, "label")= chr ""
  - attr(*, "var.labels")= chr  "systolic BP" ""

-- 

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list