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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:51:38+00:00 2026-05-20T11:51:38+00:00

#For say, I got a situation like this user_id = c(1:5,1:5) time = c(1:10)

  • 0
#For say, I got a situation like this
user_id = c(1:5,1:5)
time = c(1:10)
visit_log = data.frame(user_id, time)

#And I've wrote a method to calculate interval
interval <- function(data) {
    interval = c(Inf)
    for (i in seq(1, length(data$time))) {
        intv = data$time[i]-data$time[i-1]
        interval = append(interval, intv)
    }

    data$interval = interval
    return (data)
}

#But when I want to get intervals by user_id and bind them to the data.frame,
#I can't find a proper way
#Is there any method to get something like
new_data = merge(by(visit_log, INDICE=visit_log$user_id, FUN=interval))

#And the result should be
    user_id time interval
1        1    1      Inf
2        2    2      Inf
3        3    3      Inf
4        4    4      Inf
5        5    5      Inf
6        1    6        5
7        2    7        5
8        3    8        5
9        4    9        5
10       5   10        5
  • 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-20T11:51:38+00:00Added an answer on May 20, 2026 at 11:51 am

    We can replace your loop with the diff() function which computes the differences between adjacent indices in a vector, for example:

    > diff(c(1,3,6,10))
    [1] 2 3 4
    

    To that we can prepend Inf to the differences via c(Inf, diff(x)).

    The next thing we need is to apply the above to each user_id individually. For that there are many options, but here I use aggregate(). Confusingly, this function returns a data frame with a time component that is itself a matrix. We need to convert that matrix to a vector, relying upon the fact that in R, columns of matrices are filled first. Finally, we add and interval column to the input data as per your original version of the function.

    interval <- function(x) {
        diffs <- aggregate(time ~ user_id, data = x, function(y) c(Inf, diff(y)))
        diffs <- as.numeric(diffs$time)
        x <- within(x, interval <- diffs)
        x
    }
    

    Here is a slightly expanded example, with 3 time points per user, to illustrate the above function:

    > visit_log = data.frame(user_id = rep(1:5, 3), time  = 1:15)
    > interval(visit_log)
       user_id time interval
    1        1    1      Inf
    2        2    2      Inf
    3        3    3      Inf
    4        4    4      Inf
    5        5    5      Inf
    6        1    6        5
    7        2    7        5
    8        3    8        5
    9        4    9        5
    10       5   10        5
    11       1   11        5
    12       2   12        5
    13       3   13        5
    14       4   14        5
    15       5   15        5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I've got some TextBlocks on my UI, something like so: <StackPanel Orientation=Vertical> <TextBlock
Say I've got a multipage presentation -- a 3 page tour, for example. Each
I use this function to insert carriage returns on a string so it is
The Delphi debugger is great for debugging linear code, where one function calls other
The best example to explain my situation is to use a blog post. Let's
I have a situation where I want to use several server side controls, which
Well. This night was a very strange night to me. I am sorry to
Lets say you are dealing with your normal contact database (you know... name, phone
Rails ActiveResource is awesome ... except for one thing: as far as I can
I need a new perspective on how to design a reliable and efficient SQL

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.