R: expand data.frame values

Viewed 109

I have a data.frame like so:

df <- data.frame(x = c(998,994,992,990,989,988), y = seq(0.5, 3, by = 0.5))
df

    x   y
1 998 0.5
2 994 1.0
3 992 1.5
4 990 2.0
5 989 2.5
6 988 3.0

I would like to expand it so the values in x are exactly 1 apart so the final data.frame looks like this:

    x   y
1  998 0.5
2  997 0.5
3  996 0.5
5  995 0.5
6  994 1.0
7  993 1.0
8  992 1.5
9  991 1.5
10 990 2.0
11 989 2.5
12 988 3.0
2 Answers
Related