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

From: Frank E Harrell Jr <f.harrell_at_vanderbilt.edu>
Date: Wed 02 Aug 2006 - 05:59:47 EST

John Kane wrote:

> --- John Kane <jrkrideau@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@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

______________________________________________
R-help@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. Received on Wed Aug 02 06:20:15 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 02 Aug 2006 - 08:20:39 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.