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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:14:18+00:00 2026-06-11T14:14:18+00:00

I have a dataframe that has two sets of data that I need to

  • 0

I have a dataframe that has two sets of data that I need to multiply for a calculation. A simple version would be

sample = data.frame(apples=c(10,20,25,30,40,NA,NA,15))
sample$oranges = c(25,60,90,86,10,67,45,10)
sample$oats = c(65,75,85,95,105,115,125,135)
sample$eggs = c(23,22,21,20,19,18,17,16)
sample$consumer =c('john','mark','luke','paul','peter','thomas','matthew','brian')
sample$mealtime = c('breakfast','lunch','lunch','snack','lunch','breakfast','snack','dinner')

s1 = melt(sample,id.vars=c(5,6),measure.vars=c(1:4))

and what I’m trying to do is something along the lines of

s2 = dcast(s1, mealtime ~ ., function(x) (x[variable == 'oranges'] * x[variable =='apples'])/sum(x[variable == 'apples'])

In practice its a much longer data.frame and a more elaborate calculation but the principle should be the same. Thanks — first post to SO so apologies for any errors.

The output would be a data frame that has mealtimes as the Id var and the apple weighted average of the orange data as the values for each mealtime.

Something along the lines of

    Group.1         x
1 breakfast  1.785714
2    dinner  1.071429
3     lunch 27.500000
4     snack 18.428571

This was calculated using

sample$wa = sample$oranges*sample$apples/sum(sample$apples)
aggregate(sample$wa,by=list(sample$mealtime),sum,na.rm=T)

which feels off mathematically but was meant to be a quick kludgy approximation.

  • 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-11T14:14:19+00:00Added an answer on June 11, 2026 at 2:14 pm

    This is a much better task for plyr than it is for reshape.

    library(plyr)
    s1<-ddply(sample,.(mealtime), function(x) {return(sum(x$apples,x$oranges))})
    

    And now you have clarified the output:

    ddply(sample,.(mealtime), summarize,
          wavg.oranges = sum(apples * oranges, na.rm=TRUE) / sum(apples, na.rm=TRUE))
    #    mealtime wavg.oranges
    # 1 breakfast     25.00000
    # 2    dinner     10.00000
    # 3     lunch     45.29412
    # 4     snack     86.00000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple dataframe with two columns: df <- data.frame(x = c(1,1,2,2,3), y
I have a data frame in R that has come about from running some
I have this huge data frame that has servernames, Date, CPU, memory as the
Say I have a dataframe source <- data.frame(ID=c(1,2), COUNT=c(3,4)) that looks like this: ID
I have a data frame which has two columns of dates in the format
I have a data frame that has a format like the following: Month Frequency
I have a dataframe with a column of integers that I would like to
I have a dataframe with one column that I would like to split into
I have a dataframe and I would like to apply a function that takes
I have a data frame that is some 35,000 rows, by 7 columns. it

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.