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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:07:16+00:00 2026-06-13T10:07:16+00:00

The automatic conversion of 2D data.frame or matrix to 1D vectors, also good, makes

  • 0

The automatic conversion of 2D data.frame or matrix to 1D vectors, also good, makes some problems while writing generic functions. For example trying to filter some columns of the input foo with a command like foo[,1:3] works fine for a data.frame or matrix:

foo=matrix(1:9,nrow=3)
bar = function(x) print(x[,1])
bar(foo) # [1] 1 2 3

But consider we first want to filter some rows of foo, and it happens that only a single row remains:

bar(foo[1,]) # Error in x[, 1] : incorrect number of dimensions

This problem may be solved by a simple trick:

bar = function(x) {
    if (is.null(dim(x))) x = t(data.frame(x))
    print(x[,1])
}

But the problem would be much more complicated if we have other filters on x in the body of bar() that may convert it again to a vector. Then for every filtering we should check and do the same to ensure what we have is still a table.

The other problems are: missing row names and/or column names if the filtered part of a matrix is only a single row/column. It needs further manipulation to re-organize the filtered part as a matrix, retrieve the original row/column names and assign them to the resulting matrix.

The question is that: How to simply convert a function that works for tables a generic one, that if the input happens to be vector it still works?

  • 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-13T10:07:19+00:00Added an answer on June 13, 2026 at 10:07 am

    I have similar problems when working with data that could be a vector or a data.frame.

    Usually, I just create a subset function within the function.

    foo <- function(dat,rows) { 
    
    if(is.data.frame(dat) || is.matrix(dat)) { 
    dat <- dat[rows,] 
    } else {
    dat <- dat[rows]
    
    ... # continue with function
    }
    

    This method also maintains the class of the original object.

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

Sidebar

Related Questions

I'm using java beans with automatic data transfer and type conversion. Ex: public class
Has anyone tried automatic Java to C++ conversion for speed improvements? Is it a
The software company I work for offers data conversion as a service for new
As a learning exercise, I have been looking at how automatic type conversion works
I'm using automatic conversion from wsdl to c#, everything works apart from encoding, whenever
When does Python do automatic type conversion? -- Update: I asked this after reading
I am wondering if there is something like automatic conversion/wrapper code generation of a
While converting an app to use Automatic Reference Counting I came across this error:
I have a data frame like this: df = data.frame(a=1:3, b=2:4, c=3:5) I am
How can I disable automatic conversion of HTML tags in Rails3? I have output

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.