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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:54:33+00:00 2026-06-02T10:54:33+00:00

I have a data table the.data, where the first column indicate a measurement instrument,

  • 0

I have a data table “the.data”, where the first column indicate a measurement instrument, and the rest different measured data.

instrument <- c(1,2,3,4,5,1,2,3,4,5)
hour <- c(1,1,1,1,1,2,2,2,2,2)
da <- c(12,14,11,14,10,19,15,16,13,11)
db <- c(21,23,22,29,28,26,24,27,26,22)
the.data <- data.frame(instrument,hour,da,db)

I also have defined groups of instruments, where for example group 1 (g1) refers to instruments 1 and 2.

g1 <- c(1,2)
g2 <- c(4,3,1)
g3 <- c(1,5,2)
g4 <- c(2,4)
g5 <- c(5,3,1,2,6)
groups <- c("g1","g2","g3","g4","g5")

I need to find out at which hour the sum of each group has maximum per data type, and its sum.

g1 hour 1: sum(da)=12+14=26
g1 hour 2: sum(da)=19+15=34

So, for g1 and da the answer is hour 2 and value 34.

I did this with a for-loop within a for-loop, but it takes too long time (I interrupted after a few hours). The issue is that the.data is about 100.000 rows long and that there are about 5.000 groups with 2-50 instruments each.

What can be a good method to do this?

Sincere thanks to all contributors to Stack-overflow.

Update: Now only five groups in examples.

/Chris

  • 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-02T10:54:36+00:00Added an answer on June 2, 2026 at 10:54 am

    The group loop will have to stay, or at best be replaced by something like lapply(). The hour loop, however, can be totally replaced by reformatting to an instrument x hour matrix and then just doing vectorized algebra. For example:

    library(reshape2)
    
    groups = list(g1, g3)
    
    the.data.a = dcast(the.data[,1:3], instrument ~ hour)
    
    > sapply(groups, function(x) data.frame(max = max(colSums(the.data.a[x, -1])),
                                            ind = which.max(colSums(the.data.a[x, -1]))))
        [,1] [,2]
    max 34   45  
    ind 2    2   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a table with checkboxes in the first column. I use the jQuery
I have a table where the first column contains Strings and the second contains
I have a data table populated from a MySQL database via PHP. The first
I have a table in excel. The first column is the values for the
I have a data.table DT with a column named RF and many columns with
I have two tables in a MySQL database. In the first column of data
I have a table with 3 columns, the first column is special and contains
Background: I have a jQuery Datatable with 53x columns. The first column is the
i have two data tables with the same structure the first one has one

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.