I'd like to know if it's possible to use dplyr to expand the rows of a data.frame based on criteria in each row. If it's not possible in dplyr I'd be happy for any solution!
Here is a sample of my data
data.frame(plot=rep(c(6,7),each=4),
trans=rep(c("0,0","0,100","100,100","100,0"),2),
length_m=c(350,200,200,50,45,200,125,75) )
plot trans length_m
6 0,0 350
6 0,100 200
6 100,100 200
6 100,0 50
7 0,0 45
7 0,100 200
7 100,100 125
7 100,0 75
The data above represent two plots. In general each of my plots plot has 1 to 4 transects, identified by 0,0; 0,100; 100,100; or 100,0 (the plots above both have all four possible transects). Each transect has a length given by length_m. What I'd like to do is to divide each transect by length L, and make one row for each new transect. If the final transect is < L, then that distance should be added to the previous transect.
So, if L = 100, the above dataset would look like this
plot trans length_m
6 0,0_0 100
6 0,0_100 100
6 0,0_200 150
6 0,100_0 100
6 0,100_100 100
6 100,100_0 100
6 100,100_100 100
6 100,0_0 50
7 0,0_0 45
7 0_100_0 100
7 0,100_100 100
7 100,100_0 125
7 100,0_0 75
Note that 6, 0,0, which was 350 meters long, is split into sections 0,100 & 200, with lengths 100,100 & 150, while 6, 100,0 which was 50 meters long is just a single section 0 and is still 50 meters long.
I've tried a couple of different ways to make this work but nothing that is worth posting, so any help would be much appreciated!