简体   繁体   中英

dplyr::mutate_at iterate through columns in function

require(dplyr)
df <- data.frame(Date.time = c("2015-01-01 00:00:00", "2015-01-01 00:30:00", "2015-01-01 01:00:00", "2015-01-01 01:30:00", "2015-01-01 02:00:00"),
           RH33HMP = c(99.6,99.6,99.5,99.3,98.63),
           RH33HMP_f = c(9,9,92,93,9),
           RH38HMP = c(99.6,99.6,99.5,99.3,98.63),
           RH38HMP_f = c(9,902,9,9,91))

Here is some example data.frame. I'd like to set every value to NA where the corresponding quality column (_f) contains something else than 9 . First, I grep the column number with the actual measurements:

col_var <- grep("^Date.|_f$", names(df), invert = T)

Then I use dplyr and mutate_at with an if_else function. My problem is, that mutate_at iterates through all the columns of col_val , but the function itself does not. I tried several examples that I found on stackoverflow, but none of them seem to work.

# does not work
df_qc <- df %>%
  mutate_at(.vars = col_var,
            .funs = list(~ ifelse(df[, col_var+1] == 9, ., NA)))

i=1
df_qc <- df %>%
  mutate_at(.vars = col_var,
            .funs = list(~ ifelse(df[, i+1] == 9, ., NA)))

I think I am quite close, any help appreciated.

We can use Map :

df[col_var] <- Map(function(x, y) {y[x != 9] <- NA;y},df[col_var + 1],df[col_var])
df

#            Date.time RH33HMP RH33HMP_f RH38HMP RH38HMP_f
#1 2015-01-01 00:00:00   99.60         9    99.6         9
#2 2015-01-01 00:30:00   99.60         9      NA       902
#3 2015-01-01 01:00:00      NA        92    99.5         9
#4 2015-01-01 01:30:00      NA        93    99.3         9
#5 2015-01-01 02:00:00   98.63         9      NA        91

Similarly, you can use map2 in purrr if you prefer tidyverse .

df[col_var] <- purrr::map2(df[col_var + 1],df[col_var], ~{.y[.x != 9] <- NA;.y})

One dplyr and purrr option could be:

map2_dfr(.x = df %>%
          select(ends_with("HMP")),
         .y = df %>%
          select(ends_with("_f")),
         ~ replace(.x, .y != 9, NA)) %>%
 bind_cols(df %>%
            select(-ends_with("HMP")))

  RH33HMP RH38HMP Date.time           RH33HMP_f RH38HMP_f
    <dbl>   <dbl> <fct>                   <dbl>     <dbl>
1    99.6    99.6 2015-01-01 00:00:00         9         9
2    99.6    NA   2015-01-01 00:30:00         9       902
3    NA      99.5 2015-01-01 01:00:00        92         9
4    NA      99.3 2015-01-01 01:30:00        93         9
5    98.6    NA   2015-01-01 02:00:00         9        91

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