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 7874017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:41:09+00:00 2026-06-03T02:41:09+00:00

for starters: I searched for hours on this problem by now – so if

  • 0

for starters: I searched for hours on this problem by now – so if the answer should be trivial, please forgive me…

What I want to do is delete a row (no. 101) from a data.frame. It contains test data and should not appear in my analyses. My problem is: Whenever I subset from the data.frame, the attributes (esp. comments) are lost.

str(x)
# x has comments for each variable
x <- x[1:100,]
str(x)
# now x has lost all comments

It is well documented that subsetting will drop all attributes – so far, it’s perfectly clear. The manual (e.g. http://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.data.frame.html) even suggests a way to preserve the attributes:

## keeping special attributes: use a class with a
## "as.data.frame" and "[" method:


as.data.frame.avector <- as.data.frame.vector

`[.avector` <- function(x,i,...) {
  r <- NextMethod("[")
  mostattributes(r) <- attributes(x)
  r
}

d <- data.frame(i= 0:7, f= gl(2,4),
                u= structure(11:18, unit = "kg", class="avector"))
str(d[2:4, -1]) # 'u' keeps its "unit"

I am not yet so far into R to understand what exactly happens here. However, simply running these lines (except the last three) does not change the behavior of my subsetting. Using the command subset() with an appropriate vector (100-times TRUE + 1 FALSE) gives me the same result. And simply storing the attributes to a variable and restoring it after the subset, does not work, either.

# Does not work...
tmp <- attributes(x)
x <- x[1:100,]
attributes(x) <- tmp

Of course, I could write all comments to a vector (var=>comment), subset and write them back using a loop – but that does not seem a well-founded solution. And I am quite sure I will encounter datasets with other relevant attributes in future analyses.

So this is where my efforts in stackoverflow, Google, and brain power got stuck. I would very much appreciate if anyone could help me out with a hint. Thanks!

  • 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-03T02:41:11+00:00Added an answer on June 3, 2026 at 2:41 am

    If I understand you correctly, you have some data in a data.frame, and the columns of the data.frame have comments associated with them. Perhaps something like the following?

    set.seed(1)
    
    mydf<-data.frame(aa=rpois(100,4),bb=sample(LETTERS[1:5],
      100,replace=TRUE))
    
    comment(mydf$aa)<-"Don't drop me!"
    comment(mydf$bb)<-"Me either!"
    

    So this would give you something like

    > str(mydf)
    'data.frame':   100 obs. of  2 variables:
     $ aa: atomic  3 3 4 7 2 7 7 5 5 1 ...
      ..- attr(*, "comment")= chr "Don't drop me!"
     $ bb: Factor w/ 5 levels "A","B","C","D",..: 4 2 2 5 4 2 1 3 5 3 ...
      ..- attr(*, "comment")= chr "Me either!"
    

    And when you subset this, the comments are dropped:

    > str(mydf[1:2,]) # comment dropped.
    'data.frame':   2 obs. of  2 variables:
     $ aa: num  3 3
     $ bb: Factor w/ 5 levels "A","B","C","D",..: 4 2
    

    To preserve the comments, define the function [.avector, as you did above (from the documentation) then add the appropriate class attributes to each of the columns in your data.frame (EDIT: to keep the factor levels of bb, add "factor" to the class of bb.):

    mydf$aa<-structure(mydf$aa, class="avector")
    mydf$bb<-structure(mydf$bb, class=c("avector","factor"))
    

    So that the comments are preserved:

    > str(mydf[1:2,])
    'data.frame':   2 obs. of  2 variables:
     $ aa:Class 'avector'  atomic [1:2] 3 3
      .. ..- attr(*, "comment")= chr "Don't drop me!"
     $ bb: Factor w/ 5 levels "A","B","C","D",..: 4 2
      ..- attr(*, "comment")= chr "Me either!"
    

    EDIT:

    If there are many columns in your data.frame that have attributes you want to preserve, you could use lapply (EDITED to include original column class):

    mydf2 <- data.frame( lapply( mydf, function(x) {
      structure( x, class = c("avector", class(x) ) )
    } ) )
    

    However, this drops comments associated with the data.frame itself (such as comment(mydf)<-"I'm a data.frame"), so if you have any, assign them to the new data.frame:

    comment(mydf2)<-comment(mydf)
    

    And then you have

    > str(mydf2[1:2,])
    'data.frame':   2 obs. of  2 variables:
     $ aa:Classes 'avector', 'numeric'  atomic [1:2] 3 3
      .. ..- attr(*, "comment")= chr "Don't drop me!"
     $ bb: Factor w/ 5 levels "A","B","C","D",..: 4 2
      ..- attr(*, "comment")= chr "Me either!"
     - attr(*, "comment")= chr "I'm a data.frame"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem has been bothering me for sometime now, I have not settled on
I'm now hopeless about this problem and that's why I'm here. I'm kind of
For starters, this question is not so much about programming in the NetBeans IDE
For starters, I am completely new to PHP. That being said, here's my problem.
For starters, this is my jsfiddle: http://jsfiddle.net/gab2G/2/ <div id=div>Click</div> <a href=#>Blabla</a> <a href=#>Blabla</a> <a
For Starters, i would like to note, that this is my first post on
For starters this is homework, I just really need help with a binary search
I'm setting up Hadoop (0.20.2). For starters, I just want it to run on
Hello I want to create a dll with some functions. For starters I'm trying
I want to get 26 files (for starters): A.ico, B.ico, ... Z.ico, where they

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.