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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:47:52+00:00 2026-06-13T03:47:52+00:00

I have a data frame in R defined as follows: data frame: col 1

  • 0

I have a data frame in R defined as follows:

data frame:

col 1  col 2  col 3 col4 
200    AIG   8.5   12   
800    AIG   8.1   20.1   
500    A1B   20   50.5   
800    A1B   12   30   
120    A2M   1.6   8.5   

dat <- structure(list(col1 = c(200, 800, 500, 800, 120), col2 = structure(c(3L, 
    3L, 1L, 1L, 2L), .Label = c("A1B", "A2M", "AIG"), class = "factor"), 
        col3 = c(8.5, 8.1, 20, 12, 1.6), col4 = c(12, 20.1, 50.5, 
        30, 8.5)), .Names = c("col1", "col2", "col3", "col4"), row.names = c(NA, 
    -5L), class = "data.frame")

Then I’d like to collapse the rows by id (in this case the unique ids are A1G, A1B, A2M).
Col 1, I’d like to collapse it by adding the rows with the same id.
Col 2, I’d like to collapse it to each unique id
Col 3, I’d like to collapse it as follows, take col1*col3, add them, and then divide them by the sum of col1.
I.e., the A1G new row value should be (8.5*20+8.1*80)/(80+20). Aka the weighted average of column 3 weighted by the values of col1.
Col 4, I’d like to take the maximum value.

The resulting data frame should look like:

column 1  column 2  column 3 column 4 
800+200=1000    AIG   (8.5*200+8.1*800)/1000=8.18   max(12,20.1)=20.1   
800+500=1300    AIB   (20*800+12*500)/1300=16.9   max(50.5, 30)=50.5   
120    A2M   1.6   8.5   

Any suggestions?

  • 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-13T03:47:53+00:00Added an answer on June 13, 2026 at 3:47 am

    Here is a data.table solution that will scale well for big data (speed and memory efficient)

    library(data.table)
    DT <- data.table(dat, key="col2")
    DT[, list(col1=sum(col1), 
              col3=sum(col1 * col3) / sum(col1), 
              col4=max(col4)), by=col2]
    #   col2 col1     col3 col4
    #1:  A1B 1300 15.07692 50.5
    #2:  A2M  120  1.60000  8.5
    #3:  AIG 1000  8.18000 20.1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame that's about ts1[100, 2000] in dimension as follows: >
I have a data frame as follows, > mydata date station treatment subject par
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame with start dates and end dates, along with the
I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data frame consisting of results from multiple runs of an experiment,
I have a data frame of daily data: df with four columns: Date, A,
I have a data.frame of a time series of data, I would like to

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.