Here is a solution using 'dplyr'
> require(dplyr)
> lag<-read.table(text=" ID, y1, y2
+ 1,0,12/25/2014
+ 1,125,9/15/2015
+ 1,350,1/30/2016
+ 2,0,12/25/2012
+ 2,450,9/15/2014
+ 2,750,1/30/2016
+ 2, 656, 11/30/2016
+ ",sep=",",header=TRUE)
>
> new_lag <- lag %>%
+ mutate(y2 = as.Date(y2
I forgot about the sorting part and assumed the data.frame was already
sorted. If not, after converting y2 to class Date, you can do
lag <- lag[order(lag$ID, lag$y2), ]
Rui Barradas
Em 15-10-2016 19:45, Rui Barradas escreveu:
Hello,
Try the following.
lag<-read.table(text=" ID, y1, y2
1,0,
Hello,
Try the following.
lag<-read.table(text=" ID, y1, y2
1,0,12/25/2014
1,125,9/15/2015
1,350,1/30/2016
2,0,12/25/2012
2,450,9/15/2014
2,750,1/30/2016
2, 656, 11/30/2016
",sep=",",header=TRUE)
str(lag)
lag$y2 <- as.Date(lag$y2, format = "%m/%d/%Y")
str(lag)
# 1)
flag <- ave(lag$ID, lag$ID
Hi all,
I want sort the data by ID and Y2 then count the number of rows within
IDs. Assign a "flag" variable to reach row starting from first to
the last row.
For instance, in the following data ID "1" has three rows and each
row is assigned flag sequentially 1, 2,3.
2. In the second step, wi
4 matches
Mail list logo