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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:13:28+00:00 2026-05-16T00:13:28+00:00

I am trying to do a group by – style weighted mean in R.

  • 0

I am trying to do a “group by” – style weighted mean in R. With some basic mean the following code (using the plyr package from Hadley) worked well.

ddply(mydf,.(period),mean)

If I use the same approach with weighted.mean i get the following error “‘x’ and ‘w’ must have the same length” , which I do not understand because the weighted.mean part works outside ddply.

weighted.mean(mydf$mycol,mydf$myweight) # works just fine
ddply(mydf,.(period),weighted.mean,mydf$mycol,mydf$myweight) # returns the erros described above
ddply(mydf,.(period),weighted.mean(mydf$mycol,mydf$myweight)) # different code same story

I thought of writing a custom function instead of using weighted.mean and then passing it to ddply or even writing something new from scratch with subset. In my case it would be too much work hopefully, but there should by a smarter solution with what´s already there.

thx for any suggestions in advance!

  • 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-16T00:13:29+00:00Added an answer on May 16, 2026 at 12:13 am

    Use an anonymous function:

    > ddply(iris,"Species",function(X) data.frame(wmn=weighted.mean(X$Sepal.Length,
    +                                                               X$Petal.Length),
    +                                             mn=mean(X$Sepal.Length)))
         Species      wmn    mn
    1     setosa 5.016963 5.006
    2 versicolor 5.978075 5.936
    3  virginica 6.641535 6.588
    > 
    

    This computes a weighted mean of Sepal.Length (weighted by Petal.Length) as well as unweighted mean and returns both.

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

Sidebar

Related Questions

*I am trying to group set of data according some condition using FORTRAN code.
I'm trying to group data from the following three tables to create a new
I'm trying to group by month some rows using HQL, but I'm kind of
I'm trying to group the following string into three groups. 0:0:Awesome:awesome That being "
I'm trying to group posts from same day, the problem is that 2/20 gets
My group is trying to build a server using Apache Thrift but we are
I am trying to group row data from a 3-dimensional array by a column
Hi my code doesn't work, Im trying to group my blogentries by year and
I'm trying to group some exisiting top-level functions inside a closure (to avoid polluting
I'm trying to group related elements using class name. They, in turn, are all

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.