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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:08:10+00:00 2026-05-26T07:08:10+00:00

I have a object with some attributes whose values are integers, i.e. h =

  • 0

I have a object with some attributes whose values are integers, i.e. h =:

attr(,"foo")
[1] 4
attr(,"bar")
[1] 2

And I want to get vector of type integer(2), v =:

[1] 4 2

I have found two clumsy ways to achieve this

as.vector(sapply(names(attributes(h)), function(x) attr(h, x)))

or:

as.integer(paste(attributes(h)))

The solution I am looking for just needs to work for the basic case I described above and needs to be as fast as possible.

  • 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-05-26T07:08:11+00:00Added an answer on May 26, 2026 at 7:08 am

    Well, if you can live with the names intact:

    > h <- structure(42, foo=4, bar=2)
    > unlist(attributes(h))
    foo bar 
      4  2 
    

    Otherwise (which is actually faster!),

    > unlist(attributes(h), use.names=FALSE)
    [1]  4 2
    

    The performance is as follows:

    system.time( for(i in 1:1e5) unlist(attributes(h)) )                  # 0.39 secs
    system.time( for(i in 1:1e5) unlist(attributes(h), use.names=FALSE) ) # 0.25 secs
    system.time( for(i in 1:1e5) as.integer(paste(attributes(h))) )       # 1.11 secs
    system.time( for(i in 1:1e5) as.vector(sapply(names(attributes(h)), 
                 function(x) attr(h, x))) )                               # 6.17 secs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some object (div, input/checkbox) in an HTML page. The values for their
Ok, So I want to display some attributes, but only if they have a
I have an object User that has attributes whose accessability is declared as protected
I have some object that is instantiated in code behind, for instance, the XAML
I need to have some object hanging around between two events I'm interested in:
I am using C# 2.0 with Nunit Test. I have some object that needs
So I have an object which has some fields, doesn't really matter what. I
My current setup : I have an entity object with some properties, among them
I think I read somewhere that some modules only have object oriented interfaces (
I have a bitmap object and draw some curves on it by setpixel method.

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.