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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:50:40+00:00 2026-05-16T09:50:40+00:00

I’m using reshape in R to compute aggregate statistics over columns of a data.frame.

  • 0

I’m using reshape in R to compute aggregate statistics over columns of a data.frame. Here’s my data.frame:

> df
  a a b b ID
1 1 1 1 1  1
2 2 3 2 3  2
3 3 5 3 5  3

which is just a little test data.frame to try and understand the reshape package. I melt, and then cast, to try and find the mean of the as and the bs:

> melt(df, id = "ID") -> df.m
> cast(df.m, ID ~ variable, fun = mean)
  ID a b
1  1 1 1
2  2 2 2
3  3 3 3

Argh! What? Was hoping the mean of c(2,3) was 2.5 and so on. What’s going on? Here’s a thing:

> df.m
   ID variable value
1   1        a     1
2   2        a     2
3   3        a     3
4   1        a     1
5   2        a     2
6   3        a     3
7   1        b     1
8   2        b     2
9   3        b     3
10  1        b     1
11  2        b     2
12  3        b     3

what’s going on? Where did both my 5s go? Do I have a very basic misunderstanding going on here? If so: what is it?

  • 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-16T09:50:41+00:00Added an answer on May 16, 2026 at 9:50 am

    I updated my answer here to fix this:
    R: aggregate columns of a data.frame

    Apparently, if your data frame doesn’t have unique column names, they won’t melt properly.

    Edit:
    Instead of having column names of a a a b b, apparently you need to have unique column names for melt() to work properly. Minimally a.1 a.2 a.3 b.1 b.2, or something. After using melt(), your options to get sensible levels for variable is either to use gsub() on the levels of variable to eliminate the disambiguating values, or to use colsplit() to create two new columns. For the dummy names I just gave, that would look like:

    levels(df.m$variable) <- gsub("\\..*", "", levels(df.m$variable))
    #or
    df.m <- cbind(df.m, colsplit(df.m$variable, split = "\\.", names = c("Measure","N")))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but

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.