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

  • SEARCH
  • Home
  • 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 8421107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:03:19+00:00 2026-06-10T03:03:19+00:00

I have the following data frame x <- read.table(text = id1 id2 val1 val2

  • 0

I have the following data frame

x <- read.table(text = "  id1 id2 val1 val2
1   a   x    1    9
2   a   x    2    4
3   a   y    3    5
4   a   y    4    9
5   b   x    1    7
6   b   y    4    4
7   b   x    3    9
8   b   y    2    8", header = TRUE)

I want to calculate the mean of val1 and val2 grouped by id1 and id2, and simultaneously count the number of rows for each id1-id2 combination. I can perform each calculation separately:

# calculate mean
aggregate(. ~ id1 + id2, data = x, FUN = mean)

# count rows
aggregate(. ~ id1 + id2, data = x, FUN = length)

In order to do both calculations in one call, I tried

do.call("rbind", aggregate(. ~ id1 + id2, data = x, FUN = function(x) data.frame(m = mean(x), n = length(x))))

However, I get a garbled output along with a warning:

#     m   n
# id1 1   2
# id2 1   1
#     1.5 2
#     2   2
#     3.5 2
#     3   2
#     6.5 2
#     8   2
#     7   2
#     6   2
# Warning message:
#   In rbind(id1 = c(1L, 2L, 1L, 2L), id2 = c(1L, 1L, 2L, 2L), val1 = list( :
#   number of columns of result is not a multiple of vector length (arg 1)

I could use the plyr package, but my data set is quite large and plyr is very slow (almost unusable) when the size of the dataset grows.

How can I use aggregate or other functions to perform several calculations in one call?

  • 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-10T03:03:21+00:00Added an answer on June 10, 2026 at 3:03 am

    You can do it all in one step and get proper labeling:

    > aggregate(. ~ id1+id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) )
    #   id1 id2 val1.mn val1.n val2.mn val2.n
    # 1   a   x     1.5    2.0     6.5    2.0
    # 2   b   x     2.0    2.0     8.0    2.0
    # 3   a   y     3.5    2.0     7.0    2.0
    # 4   b   y     3.0    2.0     6.0    2.0
    

    This creates a dataframe with two id columns and two matrix columns:

    str( aggregate(. ~ id1+id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) ) )
    'data.frame':   4 obs. of  4 variables:
     $ id1 : Factor w/ 2 levels "a","b": 1 2 1 2
     $ id2 : Factor w/ 2 levels "x","y": 1 1 2 2
     $ val1: num [1:4, 1:2] 1.5 2 3.5 3 2 2 2 2
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : NULL
      .. ..$ : chr  "mn" "n"
     $ val2: num [1:4, 1:2] 6.5 8 7 6 2 2 2 2
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : NULL
      .. ..$ : chr  "mn" "n"
    

    As pointed out by @lord.garbage below, this can be converted to a dataframe with “simple” columns by using do.call(data.frame, ...)

    str( do.call(data.frame, aggregate(. ~ id1+id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) ) ) 
        )
    'data.frame':   4 obs. of  6 variables:
     $ id1    : Factor w/ 2 levels "a","b": 1 2 1 2
     $ id2    : Factor w/ 2 levels "x","y": 1 1 2 2
     $ val1.mn: num  1.5 2 3.5 3
     $ val1.n : num  2 2 2 2
     $ val2.mn: num  6.5 8 7 6
     $ val2.n : num  2 2 2 2
    

    This is the syntax for multiple variables on the LHS:

    aggregate(cbind(val1, val2) ~ id1 + id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following test data frame : test <- read.table(header = TRUE, text
I have the following R script: X <- read.table(/tmp/run178/data/monitor/portal_free_heap_monitor.log, header=T, sep=;) P1 <- subset(X,
I have the following data: a=c(1:10) b=c(16:25) c=c(24:33) wa=c(3,7,3,3,3,3,3,3,3,1) wb=c(3,2,3,3,3,3,3,3,3,8) wc=c(4,1,4,4,4,4,4,4,4,1) z=data.frame(a,b,c,wa,wb,wc) I want
I have the following data read into R as a data frame named data_old:
I have the following data.frame: sample <- data.frame(day=c(1,2,5,10,12,12,14)) sample.table <- as.data.frame(table(sample$day)) Now what I'd
Suppose I have the following data frame: Data1 X1 X2 1 15 1 2
I have the following data test<-data.frame(group=1:10, var.a=rnorm(n=10,mean=500,sd=20), var.b=runif(10)) I would like a barplot with
I have the following data.frames: a <- data.frame(id = 1:3, v1 = c('a', NA,
I have the following dataframe: DF <- data.frame(x = c(1, 2, 3,NA), y =
I have the following dummy code: dt<-data.frame(country=letters[1:20],val=rnorm(20),siz=rnorm(20)) qplot(x=country,y=val,data=dt,geom=point,size=siz) Now I want to increase the

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.