2

lets say I have a df as

ID <- c("A1","A1","A1", "A2","A2","A3", "A3", "A3")
b <- c(1,2 , 3, 1, 2, 1, 2, 3)
c <- c (100, 200, 300 ,400, 500 ,600 ,700, 800 )
df1 <- data.frame(ID,b,c)

SO the df looks like that

     a b   c
   1 A1 1 100
   2 A1 2 200
   3 A1 3 300
   4 A2 1 400
   5 A2 2 500
   6 A3 1 600
   7 A3 2 700
   8 A3 3 800

I want to move the value of c, within ID, to the next row in a new column

so the result should be like

      a b   c  new
   1 A1 1 100  NA 
   2 A1 2 200  100
   3 A1 3 300  200 
   4 A2 1 400  NA
   5 A2 2 500  400
   6 A3 1 600  NA
   7 A3 2 700  600
   8 A3 3 800  700
Mostafa Helal
  • 91
  • 1
  • 6

1 Answers1

2

We can use tidyverse

library(tidyverse)
df1 %>%
   group_by(ID) %>%
   mutate(new = lag(c))
#     ID     b     c   new
#   <fctr> <dbl> <dbl> <dbl>
#1     A1     1   100    NA
#2     A1     2   200   100
#3     A1     3   300   200
#4     A2     1   400    NA
#5     A2     2   500   400
#6     A3     1   600    NA
#7     A3     2   700   600
#8     A3     3   800   700
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 4
    basically, this is `dplyr` only, no? – ztl Feb 22 '17 at 13:00
  • @ztl Yes, but tidyverse includes more packages so, we can use other functions if necessary – akrun Feb 22 '17 at 13:01
  • 2
    OK. It makes it less readable to me, I like to know exactly where the functions I use come from, but OK... (BTW I always think that R code should include the package source like `dplyr::mutate` etc, as is a convention in Python for example, but that's another story...) – ztl Feb 22 '17 at 13:03
  • 5
    No - not from me, I would have explained it. Maybe it is because the question has been closed as duplicate - actually it is true that answering very quickly questions without checking first whether it is a duplicate could justify a downvote as it "pollutes" SO, but this downvote is not from me... – ztl Feb 22 '17 at 13:06