[R] Splitting times into groups based on a range of times

arun smartpink111 at yahoo.com
Mon Oct 14 05:38:58 CEST 2013


Hi Ben,

It looks like the condition is not met in majority of the split elements.  So, when you create a dataframe with the a column with 0 element and another column with an element, it shows the Error message.
data.frame(dt2=NULL,group=1)
#Error in data.frame(dt2 = NULL, group = 1) : 
 # arguments imply differing number of rows: 0, 1


You can do this:
lst1 <- split(df.new,df.new$start.end.group)
lst2 <- lapply(lst1,function(x){dt2=dt.new[dt.new>=x[1,1] & dt.new < x[length(x),1]]})
lst3 <- lst2[lapply(lst2,length)>0]

df1.new <- do.call(rbind,lapply(lst1[names(lst1)%in% names(lst3)],function(x) {data.frame(dt2=dt.new[dt.new>= x[1,1] & dt.new < x[length(x),1]],group=x[1,2])}))


#You could also do this from `lst3` and create groups as the names of the list elements as both are the same.
 head(df1.new)
#                     dt2 group
#61.1 2012-08-02 19:16:14    61
#61.2 2012-08-02 19:18:14    61
#61.3 2012-08-02 19:20:14    61
#61.4 2012-08-02 19:22:14    61
#61.5 2012-08-02 19:24:14    61
#61.6 2012-08-02 19:26:14    61
 tail(df1.new[df1.new$group==61,],2)
#                       dt2 group
#61.366 2012-08-03 07:26:14    61
#61.367 2012-08-03 07:28:14    61




lst1[[61]]
  #              dt2.new start.end.group
#61  2012-08-02 19:15:00              61
#200 2012-08-03 07:30:00              61


  A.K.






On Sunday, October 13, 2013 3:55 PM, Benjamin Gillespie <gybrg at leeds.ac.uk> wrote:
Hi Arun,

This is great - it works perfectly for the data I provided you.

However, I've spent almost all today trying to apply it to my real world dataset... and for some reason I keep getting the error: "Error in data.frame(dt2 = dt.new[dt.new >= x[1, 1] & dt.new < x[length(x),  : arguments imply differing number of rows: 0, 1" when trying to build df1.

It's quite odd and I can't figure out why!!

I have attached my script file and two data files. logger2.csv is used to create 'df' and discharge.csv is used to define the 'floods' (it includes river discharge data) which are then assigned ID's. As before, I want to then assign these flood id's to the relevant times in 'df'.



More information about the R-help mailing list