rdataframefunctionna

Shifting the values of specific columns to the right in R


I want to shift the values of specific columns to the right and keep the NAs on the left.

df = data.frame(a = c("one", "two", "three", "four", "five"), 
                x = c("l", "m", NA, NA, "p"),
                y = c(NA, "b", "c", NA, NA),
                z = c("u", NA, "w", "x", "y"))

      a    x    y    z
1   one    l <NA>    u
2   two    m    b <NA>
3 three <NA>    c    w
4  four <NA> <NA>    x
5  five    p <NA>    y

This is the output I want

   a      x     y     z
1  one   <NA>   l    u
2  two   <NA>   m    b 
3  three <NA>   c    w
4  four  <NA> <NA>   x
5  five  <NA>   p    y

Solution

  • You can do a row-wise apply on your df excluding the first column, combining NA with non-NA entries (by na.omit).

    df[, -1] <- t(apply(df[, -1], 1, \(x) c(rep(NA, sum(is.na(x))), na.omit(x))))
    
         a    x    y z
    1   one <NA>    l u
    2   two <NA>    m b
    3 three <NA>    c w
    4  four <NA> <NA> x
    5  five <NA>    p y