I have multicolumn data.frames with the first comumn giving ordinal observation index ( ex.: 1 4 7 9 11 13 etc). I would like to fill up the missing observations (i.e. 2 3 5 6 8 etc) with "NA"s. Thank you
Hi, see below:> dfindex value 1 1 1 2 4 6 3 7 4 4 9 5 5 11 3 6 13 2> foo <- function(x){+ index <- ifelse(x %in% df$index, df$value[which(df$index %in% x)], NA) + return(index) + }> df_ok <- data.frame(index=1:13, value=sapply(1:13, foo)) > df_okindex value 1 1 1 2 2 NA 3 3 NA 4 4 6 5 5 NA 6 6 NA 7 7 4 8 8 NA 9 9 5 10 10 NA 11 11 3 12 12 NA 13 13 2 -- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O On 19/07/07, Nikola Markov <markov at lyon.inserm.fr> wrote:> I have multicolumn data.frames with the first comumn giving ordinal > observation index ( ex.: 1 4 7 9 11 13 etc). I would like to fill up the > missing observations (i.e. 2 3 5 6 8 etc) with "NA"s. > Thank you > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Nikola Markov wrote:> I have multicolumn data.frames with the first comumn giving ordinal > observation index ( ex.: 1 4 7 9 11 13 etc). I would like to fill up the > missing observations (i.e. 2 3 5 6 8 etc) with "NA"s.Please specify reproducible examples, they make it much easier to help! If you have: df <- data.frame(index = c(1, 4, 7), x1 = 1:3, x2 = 3:1) df Then you can say: temp <- data.frame(index = 1:max(df$index)) df <- merge(temp, df, all.x = TRUE) df Uwe Ligges> Thank you > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.