简体   繁体   中英

Iteratively fill missing values

I have a data frame with one column completely filled and the other one partially filled. I would like like to fill up the s in the second column with this formula: col3 = col1*lag(col2)

When I'm using mutate, it's iterating once. I prefer using a function since I will need to apply this function over a larger sample.

Here is the data frame

dd <- tibble::tribble(~col1, ~col2,
  1.1127493, 34533.98,
  0.9432176, 32573.06,
  0.9130100, 29758.64,
  0.6593648, 19621.80,
  0.9222222, 18095.66,
  0.7349398, 13299.22,
  0.9836066, 13081.20,
  0.8518519,       NA,
  1.0000000,       NA,
  1.0000000,       NA,
  1.0000000,       NA,
  1.0000000,       NA)

I find it hard to understand the procedure you have in mind and why is it guaranteed to terminate. May be you can specify your procedure in some more detail. In the meantime, I created a procedure close the one you are asking for, to serve you as a guide, so that you can write your own solution.

iter_func <- function(c1, c2){
  c3 <- c1 + dplyr::lag(c2)
  na_pos <- which(is.na(c3))
  non_na_pos <- which(!is.na(c3))

  while(length(na_pos)){
    c3[na_pos[1]] <- c3[non_na_pos[length(non_na_pos)]]
    c3 <- c1 + c3

    na_pos <- which(is.na(c3))
    non_na_pos <- which(!is.na(c3))
  }

  c3
}

dd %>% dplyr::mutate(col3 = iter_func(col1, col2))

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM