0

I want to shift each value of a column up by one. I used shift.column() function but it kept removing the last row every time I called it.

My example dataset is:

     A     B     C
1    4     3    07/01/2019
2    5     9    08/01/2019
3    7     8    06/09/2019
4    8     1    01/23/2019
5    8     5    05/08/2019

I ran:

dataset <-shift.column(dataset, columns="C")

    A     B     C              C.shifted
1    4     3    07/01/2019     08/01/2019
2    5     9    08/01/2019     06/09/2019
3    7     8    06/09/2019     01/23/2019
4    8     1    01/23/2019     05/08/2019

The last row #5 was removed

What I want is:

    A     B     C              C.shifted
1    4     3    07/01/2019     08/01/2019
2    5     9    08/01/2019     06/09/2019
3    7     8    06/09/2019     01/23/2019
4    8     1    01/23/2019     05/08/2019
5    8     5    05/08/2019

How should I do it?

Anh Tran
  • 1
  • 1

1 Answers1

0

Do this instead of using shift.column:

dataset['C.shifted'] = dataset.C.shift(-1)
fmarm
  • 4,209
  • 1
  • 17
  • 29