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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:39:34+00:00 2026-06-15T08:39:34+00:00

I’m trying to use the aggregate function with cbind , but I must be

  • 0

I’m trying to use the aggregate function with cbind, but I must be missing something.

I’ve seen in Using Aggregate for Multiple Aggregations that I can simply define which column I want to be fixed and which I’d like to add, but I just can’t get the result I expected.

I have:

x <- data.frame(alfa = 1:9, beta = rep(1:3, 3))

  alfa beta
1    1    1
2    2    2
3    3    3
4    4    1
5    5    2
6    6    3
7    7    1
8    8    2
9    9    3

And I want to retrieve the mean of the entries aggregated by the ones in column beta. For that I’ve tried:

aggregate(cbind(alfa) ~ beta, data = x, FUN = function(x) c(gama = mean(x)) )

That gives me:

  beta alfa
1    1    4
2    2    5
3    3    6

Shouldn’t the result be something like:

  alfa beta gama
1    1    1    4
2    2    2    5
3    3    3    6

How do I force the addition of column gama? Additionally, would someone clarify the basis of the cbind() function? I’ve been struggling to understand it. Regards!

  • 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-15T08:39:35+00:00Added an answer on June 15, 2026 at 8:39 am

    Aggregate takes all elements on the left side of the ~ and uses the given function on those values, while they are grouped by the values of the right side.
    Thus, your command

    aggregate(alfa ~ beta, data=x, mean)
    

    will return the mean values of alfa grouped by beta. (As you mentioned SQL – this is the same as will happen with the SQL-clause SELECT beta, avg(alfa) FROM x GROUP BY beta)

    If you want to output the first value encountered, this basically is another aggregation that you want to do, thus your aggregation function has to return two values:

    aggregate(alfa ~ beta, data=x, function(x) c(alfa=x[1], gamma=mean(x)))
    

    (Again in SQL: SELECT beta, min(alfa), avg(alfa) FROM x GROUP BY beta)

    You asked about the cbind. As long as you have only one argument on the left hand side, this does not matter at all. But suppose you have the following situation:

    x <- data.frame(alfa = 1:9, beta = rep(1:3, 3), gamma = rnorm(9))
    

    and would like to compute, say, the mean of both columns alfa and gamma, you could do it like this:

    aggregate(cbind(alfa, gamma) ~ beta, data=x, function(x) mean(x))
    

    That way you just tell the aggregate function to use throw alfa and gamma both at the given function.

    For more and exhaustive examples, see ?aggregate.


    Edit

    You have to be careful not to mix different meanings of cbind. Used a separate function, it concats two vectors (or data.frames) of the same length to a matrix (or data.frame) with both inputs as different columns:

    > cbind(1:3, 7:9)
         [,1] [,2]
    [1,]    1    7
    [2,]    2    8
    [3,]    3    9
    

    Used in the formula notation of aggregate cbind does something related but yet different. cbind(column1, column2) just tells aggregate to use the given function on both rows seperately. Thus, something like

    aggregate(cbind(alfa, gamma) ~ beta, data=x, function(x) mean(x[,1]*x[,2]))
    

    will not work. Rather, the function will be called two times – once with the values of alfa, then with the values of beta.

    Hope that clarifies your understanding.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.