I am wondering how to write a function to replicate rows based on the value within a column, e.g. if there is a difference of > +-0.1 between one row and the next, that row is replicated so that the row value increases linearly in increments of 0.1 from the first value to the last.
I have a dataset with the following format
{
#produce dataset
df <- data.frame(site = c(5,5,5,5,5,5,5), sample = c(1,2,3,4,5,6,7), depth =c(0.4,0.5,1.1,1.5,1.7,1.4,2.2), cover=c(80,90,70,80,60,50,80))
site sample depth cover
1 5 1 0.4 80
2 5 2 0.5 90
3 5 3 1.1 70
4 5 4 1.5 80
5 5 5 1.7 60
6 5 6 1.4 50
7 5 7 2.2 60
}
So i am looking to replicate columns so that the depth column increases/decreases in 0.1 intervals.
The expanded dataset would therefore look as follows
{
Site Sample Depth Cover
1 5 1.0 0.4 80
2 5 2.0 0.5 90
3 5 2.1 0.6 90
4 5 2.2 0.7 90
5 5 2.3 0.8 90
6 5 2.4 0.9 90
7 5 2.5 1.0 90
8 5 3.0 1.1 70
9 5 3.1 1.2 70
10 5 3.2 1.3 70
11 5 3.3 1.4 70
12 5 4.0 1.5 80
13 5 4.1 1.6 80
14 5 5.0 1.7 60
15 5 6.0 1.6 50
16 5 6.1 1.5 50
17 5 6.2 1.4 50
18 5 7.0 1.5 60
19 5 7.1 1.6 60
20 5 7.2 1.7 60
21 5 7.3 1.8 60
22 5 7.4 1.9 60
23 5 7.5 2.0 60
24 5 7.6 2.1 60
25 5 7.7 2.2 60
}
Note that on line 14 the depth decreases before increasing again.
Apologies if this sort of question has been answered elsewhere but I could not find anything with this particular use case and I am not great at writing functions.
Would really appreciate if anyone could point me in the right direction.
Thanks
Oscar Flynn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.