87143

Is there any way to bind data to data.frame by some index?

#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

Answer1:

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

Recommend

  • access to property of another class
  • Query to identify gaps in time data
  • c3js - change the initial date label in x-axis
  • VAADIN 7: What is the simplest way to refresh a Vaadin View in 5 minute intervals?
  • Custom Notification Center
  • scheduledTimerWithTimeInterval is not calling at correct intervals sometimes
  • Update timer inside div. JavaScript
  • Problems with the Trapezoidal Rule
  • advanced filter array angularjs
  • Run a code after specific time in php
  • Find all combination that sum to N with multiple lists
  • How do I scale the y-axis on a histogram by the x values in R?
  • Animation: Timer vs Idle
  • How to graph streaming data in C#
  • Sum values in array of hash if they have the same value
  • Hibernate: Inheritance and relationship mapping + generics
  • Get the pasted content on document on paste event
  • How to merge keras sequential models with same input?
  • hibernate sets dirty flag (and issues update) even though client did not change value
  • Knockout custom binding handler
  • How can Delete be both a DDL and a DML statement
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • wxPython: displaying multiple widgets in same frame
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Javascript Callbacks with Object constructor
  • How to delete a row from a dynamic generate table using jquery?
  • Proper folder structure for lots of source files
  • Acquiring multiple attributes from .xml file in c#
  • How to get Windows thread pool to call class member function?
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?