Home > OS >  Flash fill possible in R?
Flash fill possible in R?

Time:05-17

How would flash filling based on observation data that is tied to another column look for R? Example

df <- data.frame(A = c(1,1,1,1,2,2,2,2), 
                   B = c('my initials1', NA, NA, NA,NA,'my initials2',NA,NA))

Is there a way to have my initials (which are tied to 1) fill down? I've tried

df |> fill(B)

But what happens is it fills down to the next observation in B. Where I'd like the fill to stop at the end of 1 based on column A instead. I was just thinking of copying the data to a separate data frame and joining it on A to achieve that.

CodePudding user response:

You can group by the first column, then it will only fill down within the group:

library(tidyverse)

df %>% 
  group_by(A) %>% 
  fill(B, .direction = "down")

Output

      A B           
  <dbl> <chr>       
1     1 my initials1
2     1 my initials1
3     1 my initials1
4     1 my initials1
5     2 NA          
6     2 my initials2
7     2 my initials2
8     2 my initials2

Or if you want to fill in for every group, then you can change the .direction argument:

df %>% 
  group_by(A) %>% 
  fill(B, .direction = "updown")

Output

      A B           
  <dbl> <chr>       
1     1 my initials1
2     1 my initials1
3     1 my initials1
4     1 my initials1
5     2 my initials2
6     2 my initials2
7     2 my initials2
8     2 my initials2

CodePudding user response:

We may use

library(dplyr)
df %>%
   group_by(A) %>%
   mutate(B = first(B[!is.na(B)])) %>%
   ungroup
# A tibble: 8 × 2
      A B           
  <dbl> <chr>       
1     1 my initials1
2     1 my initials1
3     1 my initials1
4     1 my initials1
5     2 my initials2
6     2 my initials2
7     2 my initials2
8     2 my initials2
  •  Tags:  
  • r
  • Related