[R] What's a labelled data.frame? And how do I work with it?

Frank E Harrell Jr f.harrell at vanderbilt.edu
Tue Aug 1 21:59:47 CEST 2006


John Kane wrote:
> --- John Kane <jrkrideau at yahoo.ca> wrote:
> 
>> I imported an SPSS file with its data labels using
>> spss.get (Library(Hmisc).
>> Class = data.frame
>>
>> I then updated some of the spss labels and added a
>> label to the object itself.
>>
>> label (staff.allocation) <- "raw data from the spss
>> file"
>>
>>  I then save it as an R object. When I load the
>> object
>> for further work it comes in as Class =  "labelled"
>> "data.frame"
>>  
>>
>>  Then I try this
>>
>>
> ---------------------------------------------------------
>>  # Get the raw data that we have imported from SPSS.
>> load("H:/R.objects/staff.allocation.Rdata")
>>
>> # Recode all 99's in the data base as NA except
>> dates
>> and Subject since subject
>> # 99 is a valid ID number.
>>
>> # Drop dates and ids
>> st1 <- staff.allocation[, -1:-4]
>>
>>  st1[st1==99] <- NA
>>
>> #replace date and id forget the entry date
>> st2 <-data.frame(staff.allocation$site,
>> staff.allocation$subject,
>>           staff.allocation$date, st1)
>>
>>
> -----------------------------------------------------
>> If I have applied a label to the data.frame I get an
>> error
>> Error in data.frame(staff.allocation$site,
>> staff.allocation$subject, staff.allocation$date,  : 
>>  
>>     arguments imply differing number of rows: 1865,
>> 114
>>         
>> I orgininally was getting an error about
>>  " staff.allocation$site is a labelled
>> class and cannot be coerced into a data.frame" 
>>
>> but I lost that one while trying to see what was
>> happeing
>>         
>> If I do not apply the label to staff.allocation then
>> I
>> get a Class = "data.frame and I have no problem
>> creating the second data.frame.
>>
>> Can anyone suggest what I am missing or what is
>> happening
>>
>> Thanks
> 
> To follow up to my own post it appears that if I make
> any changes in the imported labels I also get an error
> message when I attempt to create that data.frame
> The message is
> Error in as.data.frame.default(x[[i]], optional =
> TRUE) :  cannot coerce class "labelled" into a
> data.frame

No, you can do things like

library(Hmisc)
label(mydata$x) <- 'my label'

or use the upData function in Hmisc

A labelled data frame just sets up so that subsetting with [ will keep 
labels for all the variables in the data frame.

Frank

> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 


-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University



More information about the R-help mailing list