Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:55:11+00:00 2026-06-07T01:55:11+00:00

Given a data frame, DF, it is simple to save DF as an R

  • 0

Given a data frame, DF, it is simple to save DF as an R object using save() and share with co-workers. However, it is often necessary to attach a separate document explaining precise column definitions. Is there a (standard/common) way to include this information with the object?

If we built a package for DF we could create a help page explaining all these details, like the built-in datasets. Thus the data and explanation would always be available and we need only share a single package source file. However, building a package seems over-kill for this problem. (As a side benefit, we would gain version control on the data set as changes would increment the package version number).

The Hmisc package includes the label() function, which adds a new attribute to objects. Associated methods for subsetting/creating/etc data.frames are included to propagate the new attribute (since attributes are in general dropped by most functions).

Setting attributes is an obvious alternative to writing a package, and we can add arbitrarily named attributes.

A brief example:

DF <-
structure(list(Gender = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Female",
"Male"), class = "factor"), Date = structure(c(15518, 15524,
15518, 15526, 15517, 15524), class = "Date"), Dose = c(15, 10,
11, 11, 12, 14), Reaction = c(7.97755180189919, 11.7033586194156,
9.959784869289, 6.0170950790238, 1.92480908119655, 7.70265419443507
)), .Names = c("Gender", "Date", "Dose", "Reaction"), row.names = c(NA,
-6L), class = "data.frame")

library(Hmisc)

label(DF$Reaction) <- "Time to react to eye-dot test, in seconds, recorded electronically"

# or we could set our own attributes

attr(DF$Date,"Description") <- "Date of experiment. Note, results are collected weekly from test centres"

# Since Hmisc adds class "labelled" to data.frame and impelments
# the appropriate methods, the formed is retained on subsetting 
# (not that this is feature is wanted)

DF.mini <- DF[ DF$Gender=="Male",]


# compare
str(DF)      # Not quite sure why str() prints the label attribute but not the Desciptions
str(DF.mini) # we retain the label attribute

attributes(DF$Date)
attributes(DF.mini$Date) # we lose the Description attribute

So my questions:

  1. Do people include extra information with their objects (my example is a data frame, but applies to all R objects), keeping all the relevant information in one place?
  2. If yes, how?
  3. Curious, why does str() print the label attribute, I believe the Hmisc package has added another function/method somewhere, but couldn’t see why – can someone explain that bit?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:55:13+00:00Added an answer on June 7, 2026 at 1:55 am

    There is a base function: comment which can assign or retrieve text which is stored in an attribute.

    (And I do not understand the question about why does str print the label. Shouldn’t all (non-name, non-class, non-rowname) attributes be displayed by str?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sample a data frame from a given data frame such
Given data frame values are Group year Value A 2010 17 A 2011 18
Given data like this C1<-c(3,-999.000,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,4,3,6,-999.000) DF<-data.frame(ID=c(A,B,C,D,E),C1=C1,C2=C2,C3=C3) How do I go about removing
Given two data frames: C1<-c(3,4,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,6,3,7,4) DF<-data.frame(C1=C1,C2=C2,C3=C3) DF C1 C2 C3 1 3
I'd like to reshape a data frame from long to wide (dummy data given
I'd like to write some code that would take a given data frame, check
Given two data frames: df1 = data.frame(CustomerId = c(1:6), Product = c(rep(Toaster, 3), rep(Radio,
Just learning R. Given a data.frame in R with two columns, one numeric and
for a given data frame I would like to multiply values of an array
I want to draw a simple bar plot like so: test <- data.frame(y=c(1,3,53,10,30,35,50), x=c(1:7))

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.