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

  • Home
  • SEARCH
  • 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 8116159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:43:43+00:00 2026-06-06T03:43:43+00:00

I’m looking to speed up a loop that assigns a rating to rows based

  • 0

I’m looking to speed up a loop that assigns a rating to rows based on different conditions. The are six different ratings to be assigned (0 to 5) based on different conditions. I tried to do this using a for loop with if statements for each condition, but with millions of rows to go through this really is no option. I do not even know how long it took to finish. It had been running for hours though before I manually stopped it.

The rules are:

Rating 0: if df$Bounce >= 75 and df$time<10 and df$view<1
Rating 1: if df$Bounce >= 75 or df$Assist<1
Rating 2: if df$Bounce < 75 and df$Assist<2
Rating 3: if df$Bounce < 75 and df$Assist<3
Rating 4: if df$Bounce < 75 and df$Assist<=4
Rating 5: if df$Bounce < 75 and df$Assist>=5

I’ve got more of these ‘slow’ statements in my script, so the answer to this question will speed up a lot of processes!

A small example dataset

tc <- textConnection('
belongID   uniqID   Bounce     Assist   time   view    
   1           101     90       10       7       0      
   1           102     75        0       8      10
   2           103     10       30       4       2
   2           104     50        3       1      10
   2           105     74        2       5       4
   3           106      5        1       2       8  ')

df <- read.table(tc,header=TRUE)

The outcome should result in the same dataset with a new column Rating and the ratings according to the rules:

belongID   uniqID   Bounce     Assist   time   view     Rating    
   1           101     90       10       7       0       0
   1           102     75        0       8      10       1
   2           103     10       30       4       2       5
   2           104     50        3       1      10       4
   2           105     74        2       5       4       3
   3           106      5        1       2       8       2

Edit: changed rating 1 condition!

  • 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-06T03:43:44+00:00Added an answer on June 6, 2026 at 3:43 am

    Here is a simple algorithm in a function that does what you ask. Since this contains only three rules it should be really fast. (However, I make the implicit assumption that Assist is always an integer.)

    rating <- function(Bounce, Assist, time, view){
      x <- pmin(5, Assist + 1)
      x[Bounce >= 75 & time<10 & view<1] <- 0
      x[Bounce >= 75 & Assist < 1] <- 1
      x
    }
    
    within(df, rating <- rating(Bounce, Assist, time, view))
    
      belongID uniqID Bounce Assist time view rating
    1        1    101     90     10    7    0      0
    2        1    102     75      0    8   10      1
    3        2    103     10     30    4    2      5
    4        2    104     50      3    1   10      4
    5        2    105     74      2    5    4      3
    6        3    106      5      1    2    8      2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.