Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(tidyverse)
- (thewhat <- tibble(sample = 1:10L, y= 1.0, z =2.0))
- # A tibble: 10 x 3
- sample y z
- <int> <dbl> <dbl>
- 1 1 1. 2.
- 2 2 1. 2.
- 3 3 1. 2.
- 4 4 1. 2.
- 5 5 1. 2.
- 6 6 1. 2.
- 7 7 1. 2.
- 8 8 1. 2.
- 9 9 1. 2.
- 10 10 1. 2.
- (thewhere <- tibble(cond = c("a","a","b","c","a"),
- init_sample= c(1,3,4,5,7),
- duration = c(1,2,2,1,3),
- where = c(NA,"y","z","y","z")))
- # A tibble: 5 x 4
- cond init_sample duration where
- <chr> <dbl> <dbl> <chr>
- 1 a 1. 1. <NA>
- 2 a 3. 2. y
- 3 b 4. 2. z
- 4 c 5. 1. y
- 5 a 7. 3. z
- # table with the elements that should be replaced by NA
- NAs <- filter(thewhere, cond=="a") %>%
- mutate( sample = map2(init_sample, init_sample + duration - 1,seq)) %>%
- unnest %>%
- select(where, sample)
- # Takes into account the different columns but I need to manually add each relevant column
- # and another case for mutate_all when the where is NA:
- mutate(thewhat, y = if_else(sample %in% NAs$sample[NAs$where =="y"],
- NA_real_, y ))
- # A tibble: 10 x 3
- sample y z
- <int> <dbl> <dbl>
- 1 1 NA NA
- 2 2 1. 2.
- 3 3 NA 2.
- 4 4 NA 2.
- 5 5 1. 2.
- 6 6 1. 2.
- 7 7 1. NA
- 8 8 1. NA
- 9 9 1. NA
- 10 10 1. 2.
- library(tidyverse)
- lst <- NAs %>%
- split(.$where)
- set_names(names(lst), names(lst)) %>%
- map_df(., ~ thewhat[[.x]] %>%
- replace(., thewhat$sample %in% lst[[.x]]$sample, NA_real_) ) %>%
- bind_cols(thewhat %>%
- select(sample), .)
- # A tibble: 10 x 3
- # sample y z
- # <int> <dbl> <dbl>
- # 1 1 1 2
- # 2 2 1 2
- # 3 3 NA 2
- # 4 4 NA 2
- # 5 5 1 2
- # 6 6 1 2
- # 7 7 1 NA
- # 8 8 1 NA
- # 9 9 1 NA
- #10 10 1 2
Add Comment
Please, Sign In to add comment