Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 445

How to insert a new row for each missing number of one variable while filling with NA for other variables in R?

$
0
0

How to insert a new row every time there is a break in the row column numbering, while filling the other columns with NA?

Initial data:

x <- c("a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b")y <- c(1,1,2,4,4,4,7,7,11,1,3,3,3,6,6,8)dat0 <- data.frame(x,y)  >dat0   x  y1  a  12  a  13  a  24  a  45  a  46  a  47  a  78  a  79  a 1110 b  111 b  312 b  313 b  314 b  615 b  616 b  8  

Desired output:

> dat1      x  y1     a  12     a  13     a  24  <NA>  35     a  46     a  47     a  48  <NA>  59  <NA>  610    a  711    a  712 <NA>  813 <NA>  914 <NA> 1015    a 1116    b  117 <NA>  218    b  319    b  320    b  321 <NA>  422 <NA>  523    b  624    b  625 <NA>  726    b  8  

Thanks for help


Viewing all articles
Browse latest Browse all 445

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>