r - Re-expanding a compressed dataframe to include zero values in missing rows -


given dataset in following form:

> test    pos     watson crick total 1  39023      0     0     0 2  39024      0     0     0 3  39025      0     0     0 4  39026      2     1     3 5  39027      0     0     0 6  39028      0     4     4 7  39029      0     0     0 8  39030      0     1     1 9  39031      0     0     0 10 39032      0     0     0 11 39033      0     0     0 12 39034      1     0     1 13 39035      0     0     0 14 39036      0     0     0 15 39037      3     0     3 16 39038      2     0     2 17 39039      0     0     0 18 39040      0     1     1 19 39041      0     0     0 20 39042      0     0     0 21 39043      0     0     0 22 39044      0     0     0 23 39045      0     0     0 

i can compress these data remove 0 rows following code:

a=subset(test, total!=0)  >     pos     watson crick total  4  39026      2     1     3  6  39028      0     4     4  8  39030      0     1     1  12 39034      1     0     1  15 39037      3     0     3  16 39038      2     0     2  18 39040      0     1     1 

how code reverse transformation? i.e. convert dataframe a original form of test.

more specifically: without access original data, how re-expand data (to include sequential "pos" rows) arbitrary range of pos?

here, id column irrelevant. in real example, id numbers row numbers created r. in real example, compressed dataset have sequential id numbers.

here's possibility, using base r. unless explicitly provide initial , final value of pos, first , last index value in restored dataframe correspond values given in "compressed" dataframe a:

restored <- data.frame(pos=(a$pos[1]:a$pos[nrow(a)])) # change range if required restored <- merge(restored,a, all=true) restored[is.na(restored)] <- 0 #> restored #     pos watson crick total #1  39026      2     1     3 #2  39027      0     0     0 #3  39028      0     4     4 #4  39029      0     0     0 #5  39030      0     1     1 #6  39031      0     0     0 #7  39032      0     0     0 #8  39033      0     0     0 #9  39034      1     0     1 #10 39035      0     0     0 #11 39036      0     0     0 #12 39037      3     0     3 #13 39038      2     0     2 #14 39039      0     0     0 #15 39040      0     1     1 

possibly last step can combined merge function using na.action option correctly, didn't find out how.


Comments