shift rows above

Data i have


id   <- c(1,1,1,2,2,2,2,3,3,4,4,4)
time <- c(0,2,4,0,4,7,10,0,5,0,3,8)
dv   <- c(NA,2,4,NA,4,8,9,NA,5,NA,6,6)
tad  <- c(1,0,3,1,2,0,5,0,1,1,6,0)

df <- data.frame(id,time,dv,tad)

if tad == 0 , i want to shift the whole row 1 step above.

please help me to do this. thank you.

i have some factor and character columns as well in original dataset

Hi @sai_matcha,
Does this reprex do what you require?

id   <- c(1,1,1,2,2,2,2,3,3,4,4,4)
time <- c(0,2,4,0,4,7,10,0,5,0,3,8)
dv   <- c(NA,2,4,NA,4,8,9,NA,5,NA,6,6)
tad  <- c(1,0,3,1,2,0,5,0,1,1,6,0)

df <- data.frame(id,time,dv,tad)
df
#>    id time dv tad
#> 1   1    0 NA   1
#> 2   1    2  2   0
#> 3   1    4  4   3
#> 4   2    0 NA   1
#> 5   2    4  4   2
#> 6   2    7  8   0
#> 7   2   10  9   5
#> 8   3    0 NA   0
#> 9   3    5  5   1
#> 10  4    0 NA   1
#> 11  4    3  6   6
#> 12  4    8  6   0

for(ii in 2:length(df$tad)) {
  ifelse(df$tad[ii] == 0,
  df[ii-1,] <- df[ii,],
  df[ii-1,] <- df[ii-1,])
}

df
#>    id time dv tad
#> 1   1    2  2   0
#> 2   1    2  2   0
#> 3   1    4  4   3
#> 4   2    0 NA   1
#> 5   2    7  8   0
#> 6   2    7  8   0
#> 7   3    0 NA   0
#> 8   3    0 NA   0
#> 9   3    5  5   1
#> 10  4    0 NA   1
#> 11  4    8  6   0
#> 12  4    8  6   0

Created on 2020-11-30 by the reprex package (v0.3.0)

This solution won't win any prizes for elegance! Would be good to see how this could be achieved in dplyr.

HTH

1 Like

Could you clarify what you mean with 'shift the whole row 1 step above"? E.g. how would the first row with tad==0 have to look like eventually? Do you mean adding 1 to all the other values?

my solution

id   <- c(1,1,1,2,2,2,2,3,3,4,4,4)
time <- c(0,2,4,0,4,7,10,0,5,0,3,8)
dv   <- c(NA,2,4,NA,4,8,9,NA,5,NA,6,6)
tad  <- c(1,0,3,1,2,0,5,0,1,1,6,0)

df <- data.frame(id,time,dv,tad)

library(tidyverse)

(df2<- mutate(df,orig_row_n=row_number()))

df2%>% mutate(new_row = ifelse(tad==0,orig_row_n- 1.5,
                                               orig_row_n)) %>% arrange(new_row)
   id time dv tad orig_row_n new_row
1   1    2  2   0          2     0.5
2   1    0 NA   1          1     1.0
3   1    4  4   3          3     3.0
4   2    0 NA   1          4     4.0
5   2    7  8   0          6     4.5
6   2    4  4   2          5     5.0
7   3    0 NA   0          8     6.5
8   2   10  9   5          7     7.0
9   3    5  5   1          9     9.0
10  4    0 NA   1         10    10.0
11  4    8  6   0         12    10.5
12  4    3  6   6         11    11.0
4 Likes

Hi ..

  1. given the condition , TAD of first row will never be zero in my case

  2. assume we have row numbers for data. if row value in TAD column is zero, decrease the row number( if it is 19 , make it 18). so actual row will shift up for one step. and above row come down for one step

Thank you.

Sorry for my late reply. I tested all the solutions. those worked very well for my data. I am just confused, to mark which one as solution :sweat_smile:.

Thank you very much for your kind help and time.

I am available if anymore clarifications are required.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.