0

I have a data.frame with two columns. The first column contains various specific times during a day. The second column contains the animal behavior (behavior period) that I observed at each specific time:

Time; Behavior

10:20; feeding

10:25; feeding

10:30; resting
...

For each of those behavior periods I have an additional dataset (TimeSeries) which contains data about the actual animal movement (output from a movement sensor). Each TimeSeries has about 100 rows:

Time; Var1; Var2

10:20:01; 1345; 5232

10:20:02; 1423; 5271

...

Now I would like to link each TimeSeries with the behavior from the first dataset. So, that R knows that "feeding" is related to the TimeSeries of 10:20 and 10:25 and that "resting" is related to the TimeSeries of 10:30 and so on.

Afterwards I want to use this "knowledge" to calculate mean and sd from each TimeSeries. So I will have all the means and sd's from all TimeSeries for each behavior.

Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
  • Please provide usable (R code) complete reproducible examples, with also the expected output of the solution you expect. – Karl Forner Jul 01 '13 at 16:39
  • Try [this](http://stackoverflow.com/questions/15303283/how-to-do-vlookup-and-fill-down-like-in-excel-in-r) – Metrics Jul 01 '13 at 17:01

1 Answers1

0

It is not clear whether your times are currently characters, factors, POSIXct, variables, etc. So you should first convert them (possibly in a new column) to a numeric variable, something like the number of seconds since midnight. Functions like strptime, difftime, and as.numeric may help.

Add a column to the first data frame that is just 1:nrow(firstdf). Then add a column to the second dataframe that is computed by the findInterval function:

seconddf$newcol <- findInterval( seconddf$seconds, firstdf$seconds )

Now you can merge the 2 data frames on the new columns and the finer grained times will be associated with the activity from the most recent time.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110