Home > Software design >  Inserting a row in between consecutive rows based on a condition in R
Inserting a row in between consecutive rows based on a condition in R

Time:12-14

I am populating a data frame in R with 0s and 1s and, in some cases, I need to insert a row between two consecutive rows as long as a condition is met. The condition would be that if Column C == 1 (first row; i) or Column A == 1 (consecutive row, i 1), the row to be inserted in between them should have a 1 in Column B and 0s in Columns A and C. This condition should also apply in the opposite way (if Column A == 1 and the consecutive row has Column C == 1)

Column A Column B Column C
1 0 0
0 1 0
0 0 1
0 0 1
1 0 0

Considering that my data wrangling skills are not that advanced, I tried with the functions complete() and fill(), but without further luck.

Thank you very much community!!

CodePudding user response:

So I did not understand the condition, because first you say OR and after AND, but I created a code that you can use and adapt to the condition you want.

Data

df <-
  tibble::tribble(
    ~Column.A, ~Column.B, ~Column.C,
           1L,        0L,        0L,
           0L,        1L,        0L,
           0L,        0L,        1L,
           0L,        0L,        1L,
           1L,        0L,        0L
    )

Code

library(tidyr)
library(dplyr)

df %>%
  mutate(aux = row_number()) %>% 
  bind_rows(
    df %>% 
      mutate(aux = row_number()   .5) %>% 
      filter((Column.A == 1 & lead(Column.C) == 1) | (Column.C == 1 & lead(Column.A) == 1)) %>% 
      mutate(Column.B = 1, Column.A = 0, Column.C = 0)
  ) %>% 
  arrange(aux)

Output

# A tibble: 6 x 4
  Column.A Column.B Column.C   aux
     <dbl>    <dbl>    <dbl> <dbl>
1        1        0        0   1  
2        0        1        0   2  
3        0        0        1   3  
4        0        0        1   4  
5        0        1        0   4.5
6        1        0        0   5 
  • Related