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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:25:36+00:00 2026-05-25T20:25:36+00:00

Consider the following code: A <- matrix(1:12, ncol=4) colnames(A) <- letters[1:4] class(A) <- c("foo",

  • 0

Consider the following code:

A <- matrix(1:12, ncol=4)
colnames(A) <- letters[1:4]
class(A) <- c("foo", "matrix")

when A is subset, it loses the "foo" class label:

class(A[1:2,])
# [1] "matrix"

The same happens with vectors. Yet, the same doesn’t happen with data.frames:

B <- as.data.frame(A)
class(B) <- c("foo", "data.frame")
class(B[1:2,])
# [1] "foo"        "data.frame"

And usually, applying generic functions to objects preserves the class attribute. Not for matrix/numeric/integer objects. Why? And can this behavior be avoided?

  • 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-25T20:25:37+00:00Added an answer on May 25, 2026 at 8:25 pm

    data.frames have their own subset method [.data.frame, which takes care of the class for you. I’m not sure why the Primitive doesn’t preserve the class, but it’s pretty straight-forward to create your own subset method.

    `[.foo` <- function(x, i, j, ...) {
      y <- unclass(x)[i,j,...]
      class(y) <- c("foo",class(y))
      y
    }
    class(A[1:2,])
    # [1] "foo"    "matrix"
    

    EDIT:
    As others have mentioned, NextMethod should be used here.

    `[.foo` <- `[.bar` <- function(x, i, j, ...) {
      y <- NextMethod(.Generic)
      class(y) <- .Class
      y
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider the following code: public class Vehicle { public void StartEngine() { // Code
Consider the following code: def create_class(class_name, superclass, &block) klass = Class.new superclass, &block Object.const_set
Consider the following code: object foo { trait Bar[Q[_]] implicit object OptionBar extends Bar[Option]
Consider the following code in Python: class A(object): CLASS_ATTRIBUTE = 42 def f(self): return
Consider following code public class City { public string Name { get { return
Consider the following code, public class StartUp { public StartUp(String[] test){} public static void

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.