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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:50:29+00:00 2026-05-21T07:50:29+00:00

R Version 2.11.1 32-bit on Windows 7 I get the data train.txt as below:

  • 0

R Version 2.11.1 32-bit on Windows 7

I get the data train.txt as below:

USER_A USER_B ACTION
1        7      0
1        8      1
2        6      2
2        7      1
3        8      2

And I deal with the data as the algorithm below:

train_data=read.table("train.txt",header=T)
result=matrix(0,length(unique(train_data$USER_B)),2)
result[,1]=unique(train_data$USER_B)
for(i in 1:dim(result)[1])
{
    temp=train_data[train_data$USER_B%in%result[i,1],]
    result[i,2]=sum(temp[,3])/dim(temp)[1]
}

the result is the score of every USER_B in train_data. the score is defined as:

score of USER_B=(the sum of all the ACTION of USER_B)/(the recommend times of USER_B)

but the train_data is very large, it may take me three days to finish this program, so I come here to ask for help, could this algorithm be improved?

  • 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-05-21T07:50:30+00:00Added an answer on May 21, 2026 at 7:50 am

    Running your example, your desired result is to calculate the mean ACTION for each unique USER_B:

         [,1] [,2]
    [1,]    7  0.5
    [2,]    8  1.0
    [3,]    6  2.0
    

    You can do this with one line of code using the ddply() function in package plyr

    library(plyr)
    ddply(train_data[, -1], .(USER_B), numcolwise(mean))
    
      USER_B ACTION
    1      6    2.0
    2      7    0.5
    3      8    1.0
    

    Alternatively, the function tapply in base R does the same:

    tapply(train_data$ACTION, train_data$USER_B, mean)
    

    Depending on the size of your table, you can get an improvement in execution time of 20x or higher. Here is the system.time test for a data.frame with a million entries. Your algorithm takes 116 seconds, ddply() takes 5.4 seconds, and tapply takes 1.2 seconds:

    train_data <- data.frame(
            USER_A = 1:1e6,
            USER_B = sample(1:1e3, size=1e6, replace=TRUE),
            ACTION = sample (1:100, size=1e6, replace=TRUE))
    
    yourfunction <- function(){
        result <- matrix(0,length(unique(train_data$USER_B)),2)
        result[,1] <- unique(train_data$USER_B);
        for(i in 1:dim(result)[1]){     
            temp=train_data[train_data$USER_B%in%result[i,1],]
            result[i,2]=sum(temp[,3])/dim(temp)[1]
        }
        result
    }
    
    system.time(XX <- yourfunction())
       user  system elapsed 
     116.29   14.04  134.33 
    
    system.time(YY <- ddply(train_data[, -1], .(USER_B), numcolwise(mean)))
       user  system elapsed 
       5.43    1.60    7.19 
    
    system.time(ZZ <- tapply(train_data$ACTION, train_data$USER_B, mean))
       user  system elapsed 
       1.17    0.06    1.25 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

R Version 2.11.1 32-bit on Windows 7 I get a data set as below:
I am running 64-bit Windows 7 and the Python 2.6 installation (64-bit version). In
1) I have installed 32 bit version wamp 2.2E, on windows XP 32bit. The
To preface, I am on Windows 7 (64-bit), running Java version 6 (update 33)
I am usint the 64-bit version of Enthought Python to process data across multiple
In x64 bit version of windows, i see that are also x86 bit directories.
When Matlab is installed in its 64 bit version, it can use only 64
Specifically curious if IIS 5.1 has a 64 bit version, but I'm guessing only
Is there a way to determine the JVM version (32-bit or 64-bit) in C#?
I am using Jasper Server 4.5.0 32-bit version. When I click on Next after

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.