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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:21:02+00:00 2026-06-06T19:21:02+00:00

I would like to optimize the following code in R. This loop takes a

  • 0

I would like to optimize the following code in R. This loop takes a long time to run.
I wonder if someone could help me to optimize this code because it takes ages to run?
I thank you all!

SIN_FM5 : is a database with about 300.000 rows and 7 columns.

# Make Combination  

SIN_FM5$Combination=(SIN_FM5$SINISTRE)
Count.Comb=data.frame(table(SIN_FM5$Combination))


# Calculate number of combinations 
Total.Comb=nrow(Count.Comb)

# Loop through all combinations and calculate statistics

Statistics=array(0,dim=c(Total.Comb,5))

for (i in 1:Total.Comb) {

  Subset=subset(SIN_FM5, SIN_FM5$Combination==Count.Comb[i,1])
  Statistics[(i),]=c(Count.Comb[i,1],mean(Subset$MONTANT_PAIEMENT), 
  median(Subset$MONTANT_PAIEMENT),min(Subset$MONTANT_PAIEMENT), 
  max(Subset$MONTANT_PAIEMENT))

}
resultatN=cbind(Count.Comb,Statistics)

dput(head(SIN_FM5))

Console~/

    "TRSP-5194", "TRSP-5197", "TRSP-5201", "TRSP-5202", "TRSP-5204", 
"TRSP-5205", "TRSP-5207", "TRSP-5212", "TRSP-5214", "TRSP-5215", 
"TRSP-5218", "TRSP-5222", "TRSP-5230", "TRSP-5238", "TRSP-5243", 
"TRSP-5247", "TRSP-5248", "TRSP-5253", "TRSP-5254", "TRSP-5255", 
"TRSP-5257", "TRSP-5259", "TRSP-5262", "TRSP-5263", "TRSP-5266", 
"TRSP-5267", "TRSP-5268", "TRSP-5270", "TRSP-5271", "TRSP-5274", 
"TRSP-5277", "TRSP-5279", "TRSP-5281", "TRSP-5283", "TRSP-5288", 
"TRSP-5289", "TRSP-5293", "TRSP-5296", "TRSP-5299", "TRSP-5301", 
"TRSP-5303", "TRSP-5304", "TRSP-5306", "TRSP-5308", "TRSP-5310", 
"TRSP-5311", "TRSP-5312", "TRSP-5313", "TRSP-5335", "TRSP-5343", 
"TRSP-5348", "TRSP-5352", "TRSP-5357", "TRSP-5363", "TRSP-5366", 
"TRSP-5372", "TRSP-5373", "TRSP-5384", "TRSP-5386", "TRSP-5388", 
"TRSP-5391", "TRSP-5392", "TRSP-5428", "TRSP-5436", "VANBILSENYolanda", 
"VanLierop", "VirgaJesseZiekenhuis", "WanetGeorges", "WANETThierry", 
"WILLEMSMichel", "WUESTENBERGHSAlain", "X01", "X01CR", "X02CR", 
"X03CR", "X04CR", "X05CR", "X06CR", "X07CR", "Y01", "Y01CR", 
"Y02", "ZOPO-5344"), class = "factor"), Combination = c(73010009L, 
73010009L, 73010014L, 73010014L, 73010014L, 73010014L)), .Names = c("SINISTRE", 
"victimeid", "Nature.Injury", "LocationL", "DurationITT", "Code.Nace2008", 
"POLICE", "TYPE_DE_PAIEMENT", "MODE_DE_PAIEMENT", "CODE_NATURE_DE_PAIEMENT", 
"MONTANT_PAIEMENT", "BENEFICIARE", "Combination"), row.names = c(NA, 
6L), class = "data.frame")
  • 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-06T19:21:03+00:00Added an answer on June 6, 2026 at 7:21 pm
    SIN_FM5 <- data.frame(Combination = sample(1:10, 100, repl=TRUE), MONTANT_PAIEMENT=rnorm(100))
    bySIN <- by(SIN_FM5, list(SIN_FM5[['Combination']]), FUN= function(subd) { 
               data.frame(counts = nrow(subd), 
                    meanMont = mean(subd$MONTANT_PAIEMENT), 
                    medMont =  median(subd$MONTANT_PAIEMENT),
                    minMont = min(subd$MONTANT_PAIEMENT), 
                    maxMont =  max(subd$MONTANT_PAIEMENT) )  } )
    > sapply(bySIN, as.vector)
             1         2           3          4          5          6          7          8         
    counts   11        7           14         9          11         16         10         8         
    meanMont 0.3499753 -0.188964   0.1740817  -0.1505312 -0.6335896 -0.1434513 -0.2148642 -0.2978299
    medMont  0.4381513 -0.06965143 0.05762425 -0.2247187 -0.7682626 -0.2288606 -0.1467318 -0.3315809
    minMont  -1.122418 -0.9638749  -1.634259  -1.336908  -2.068224  -1.974108  -2.15415   -1.295045 
    maxMont  1.50662   0.1653189   1.215114   1.243138   0.4643551  1.29805    1.154282   0.7097163 
             9          10       
    counts   4          10       
    meanMont 0.2575141  0.146613 
    medMont  0.07692888 0.1047567
    minMont  -0.534418  -1.006938
    maxMont  1.410617   1.4973 
    

    Here’s a data.table solution. Likely to be much faster:

    require(data.table)
    dtb <- data.table(SIN_FM5)
    setkey(dtb, "Combination")
    
    dtb[ , list(counts=length(MONTANT_PAIEMENT),
                   meanMont = mean(MONTANT_PAIEMENT), 
                   medMont =  median(MONTANT_PAIEMENT),
                   minMont = min(MONTANT_PAIEMENT), 
                   maxMont =  max(MONTANT_PAIEMENT) ), by="Combination"]
    #-----------------------------------------------
          Combination counts   meanMont     medMont    minMont   maxMont
     [1,]           1     11  0.3499753  0.43815133 -1.1224179 1.5066198
     [2,]           2      7 -0.1889640 -0.06965143 -0.9638749 0.1653189
     [3,]           3     14  0.1740817  0.05762425 -1.6342586 1.2151136
     [4,]           4      9 -0.1505312 -0.22471868 -1.3369085 1.2431378
     [5,]           5     11 -0.6335896 -0.76826264 -2.0682244 0.4643551
     [6,]           6     16 -0.1434513 -0.22886060 -1.9741083 1.2980502
     [7,]           7     10 -0.2148642 -0.14673175 -2.1541500 1.1542819
     [8,]           8      8 -0.2978299 -0.33158086 -1.2950452 0.7097163
     [9,]           9      4  0.2575141  0.07692888 -0.5344180 1.4106168
    [10,]          10     10  0.1466130  0.10475674 -1.0069382 1.4972998
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to optimize the following code. Currently it runs around 0.085 seconds
I would like to optimize following lines of code for Sorting. public ViewResult Index(string
I came across the following code recently and would like to optimize it: Public
I would like to optimize the following method, that returns the total file count
I would like to use something like AutoMapper for the following code instead of
i would like to know whether the following query is optimized? select SUM(payments.paid_amt) as
I have a MySQL query that I would like to optimize a little bit.
I would like to implement an B+ tree in Java and try to optimize
Would like a for loop in jquery so that: For every hover_link: show hidden
Would like someone to take a look at my script and tell me where

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.