Question

I have a data frame as seen below with over 1000 rows. I would like to subset the data into bins by 1m intervals (0-1m, 1-2m, etc.). Is there an easy way to do this without finding the minimum depth and using the subset command multiple times to place the data into the appropriate bins?

Temp..ºC. Depth..m. Light  time       date
1     17.31     -14.8   255 09:08 2012-06-19
2     16.83     -21.5   255 09:13 2012-06-19
3     17.15     -20.2   255 09:17 2012-06-19
4     17.31     -18.8   255 09:22 2012-06-19
5     17.78     -13.4   255 09:27 2012-06-19
6     17.78      -5.4   255 09:32 2012-06-19
Was it helpful?

Solution

Assuming that the name of your data frame is df, do the following:

split(df, findInterval(df$Depth..m., floor(min(df$Depth..m.)):0))

You will then get a list where each element is a data frame containing the rows that have Depth..m. within a particular 1 m interval.

Notice however that empty bins will be removed. If you want to keep them you can use cut instead of findInterval. The reason is that findInterval returns an integer vector, making it impossible for split to know what the set of valid bins is. It only knows the values it has seen and discards the rest. cut on the other hand returns a factor, which has all valid bins defined as levels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top