data is below
dialled Ringing state duration NA NA NA 0 NA NA NA 0 NA NA NA 0 NA NA NA 0 123 NA NA 0 123 NA NA 0 123 NA NA 0 123 NA NA 60 NA NA active 0 NA NA active 0 NA NA inactive 0 NA NA inactive 0 123 NA inactive 0 123 NA inactive 0 123 NA inactive 0 NA NA inactive 0 NA NA inactive 0 NA NA inactive 0 222 NA inactive 0 222 NA inactive 0 222 NA inactive 37 NA NA active 0 NA NA active 0 NA NA inactive 0 123 NA inactive 0 123 NA inactive 0 123 NA active 60 NA NA active 0 NA NA active 0 NA NA active 0 NA NA active 0 123 NA inactive 0 123 NA inactive 0 123 NA inactive 0 answer i am looking for is dialled Ringing state duration 123 NA NA 0 123 NA NA 60 123 NA inactive 0 123 NA inactive 0 222 NA inactive 0 222 NA inactive 37 123 NA inactive 0 123 NA inactive 60 123 NA inactive 0 123 NA inactive 0
also If you can help me to get the immediate next row, after last row of every by group and Rbind them
In
data.table v1.9.5
, there's a new functionrleid()
that helps accomplish this task fairly straightforward. You can install it by following these instructions:.SD
contains the subset of data for groups specified inby =
.You could create a grouping variable "grp" (similar as here). Subset the rows of "df" that are not '0' for "grp", use
slice
to get the first and last row for each "grp",ungroup
and remove thegrp
variable.Or a
base R
option would be to get the row index of first and last rows of subset dataset "df1" based on "grp" and then use it to extract the rows.Update
It is not clear from the comments. Perhaps this helps
data