-1

I have a data frame with 2 columns. Patient_Id and time (when visit the doctor). I would like to add a new column "timestart" which have 0 at the first row for each different Patient_id and the other rows with the same id have the preview value from column time. I think to do this with loop for, but I am new user in R and I don’t know how. Thanks in advance.

George
  • 5
  • 3
  • To select all-but-last values from a "vector" -say `x = 1:4`-, you can use `x[-length(x)]` so something like `cbind(x, c(NA, x[-length(x)]))` shows `x` and its previous value. To perform operations by group see [here](http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega). You could, also, add a more [specific example with the expected output](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – alexis_laz Jul 10 '16 at 16:14
  • sorry it’s my mistake. Your answer is very useful. Thank you so much. – George Jul 10 '16 at 16:16

1 Answers1

0

We can group by 'Patient_id' and create the new column with the lag of 'time'

library(dplyr)
df1 %>%
   group_by(Patient_id) %>%
   mutate(timestart = lag(time, default = 0))
#    Patient_id  time timestart
#        <int> <int>  <int>
#1          1     1      0
#2          1     2      1
#3          1     3      2
#4          2     1      0
#5          2     2      1
#6          2     3      2

data

df1 <- data.frame(Patient_id = rep(1:2, each = 3), time = 1:3)
akrun
  • 874,273
  • 37
  • 540
  • 662