I have a data frame with a sequence in 'col1' and values in 'col2':
col1 col2
2 0.02
5 0.12
9 0.91
13 1.13
I want to expand the irregular sequence in 'col1' with a regular sequence from 1 to 13. For the values in 'col1' which are missing in the original data, I want 'col2' to have the value 0
in the final output:
col1 col2
1 0
2 0.02
3 0
4 0
5 0.12
6 0
7 0
8 0
9 0.91
10 0
11 0
12 0
13 1.13
How can I do this in R?
Another way would be the following. Your data is called
mydf
here. You create a data frame with a column including 1 to the max value ofcol1
. Then, you use assign the values ofcol2
inmydf
to a new column calledcol2
infoo
. You use the numbers incol1
inmydf
as index when you do this process. By this time, you haveNA
incol2
infoo
. You want to change NA to 0. So the final step is to do this. You look for NA's position incol2
infoo
usingis.na()
and assign zeros to the positions.Taking lmo's idea into an account, you can create a data frame with 0 first and avoid the 3rd step.
DATA
Just for completeness, a self binary join using
data.table
(you will getNA
s instead of zeroes, but that could be easily changed if needed)Just to add a different point of view, consider that what you have can be seen as a sparse vector, i.e. a vector whose only the non-zero values are defined. Sparse vectors are implemented by the
Matrix
package in R. Ifdf
is your initialdata.frame
, try:The same result in a one-liner
base
R: