[R] insert missing values in a sequence

Mike Cheung mikewlcheung at hku.hk
Wed Jan 12 08:54:38 CET 2005


Hello.

I have a data frame with two variables where x are incomplete sequences. 
I would like to see how I could complete the sequences in x and insert 
the missing values in y.

# Example data missing 5 and 7
x <- c(1:5, 8:10)
y <- rnorm(8)
data <- data.frame(x,y)
data
# Output
#   x          y
#1  1 -1.0693219
#2  2  0.9134950
#3  3  1.3231290
#4  4 -1.1376213
#5  5  0.5367879
#6  8  0.9595294
#7  9 -0.3254167
#8 10  0.1358020

# Expected output after some manipulations
    x          y
1  1 -1.0693219
2  2  0.9134950
3  3  1.3231290
4  4 -1.1376213
5  5  0.5367879
6  6         NA
7  7         NA
8  8  0.9595294
9  9 -0.3254167
10 10  0.1358020

Thanks a lot in advance!

Best,
Mike




More information about the R-help mailing list