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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:08:04+00:00 2026-06-04T01:08:04+00:00

I had a question regarding the colMeans function. Is there a version of this

  • 0

I had a question regarding the colMeans function. Is there a version of this that will not return an error when it runs into a column of length one? For example

temp<-cbind(c(2,2),c(3,4))
colMeans(temp)

[1] 2.0 3.5

But for this one

temp2<-c(2,2)
colMeans(temp2)

Error in colMeans(temp2) : 
'x' must be an array of at least two dimensions

But, if I apply the function mean to each column it properly comes up with the value of 2 and 2.

I wrote a function to do this

testfun<-function(i,x){
mean(x[,i])
}
sapply(1:ncol(x),testfun,x)

which gives the same results as colMeans.
I’ve heard that colMeans is supposed to be much faster than this method. So, is there a version of colMeans that will work when my column is of size 1.

  • 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-04T01:08:06+00:00Added an answer on June 4, 2026 at 1:08 am

    As @Paul points out, colMeans expects “an array of two or more dimensions” for its x argument (from ?colMeans). But temp2 is not an array

    is.array(temp2)
    # [1] FALSE
    

    temp2 can be made into an array:

    (tempArray <- array(temp2, dim = c(1, 2)))
    #      [,1] [,2]
    # [1,]    2    2
    
    colMeans(tempArray)
    # [1] 2 2
    

    Perhaps temp2 came from subsetting an array, such as

    array(temp2, dim = c(2, 2))[1, ]
    

    But this is not an array. To keep it as an array, add drop = FALSE inside the brackets:

    array(temp2, dim = c(2, 2))[1, , drop = FALSE]
    #      [,1] [,2]
    # [1,]    2    2
    

    Then you can use colMeans on the subsetted array.

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

Sidebar

Related Questions

I was reading this post and had a question that I didn't think it
Just had a question regarding the UILabel class. I know that the UITextField control
I had a quick question regarding RegEx... I have a string that looks something
I just had a quick question regarding loops in Ruby. Is there a difference
I am going to paste a code here and had a question regarding that
I was tryig to learn .htaccess, and and had a question regarding this, i
I had a question regarding the split function used in Visual Basic. As of
I have a question this time around regarding the Android custom camera, NOT the
There was a question regarding passing taking the following function template, and instantiating it
I had a question regarding the BASE version in svn. I know it is

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.