r - Select only specific cases from dataframe -
how can select rows followed same value in specific column?
i.e. (using reference values of v1)
`v1 = c(1,1,2,3,1,2,4,1,1,2,3,4) v2 = seq(1:12) v3 = c(rep("blue", 4), rep("red", 4), rep("green", 4)) df<- data.frame(v1, v2, v3) df` > df v1 v2 v3 1 1 1 blue 2 1 2 blue 3 2 3 blue 4 3 4 blue 5 1 5 red 6 2 6 red 7 4 7 red 8 1 8 red 9 1 9 green 10 2 10 green 11 3 11 green 12 4 12 green
to this; cases 1 , 8, followed cases value v1 1 saved
v1 v2 v3 1 1 1 blue 8 1 8 red
you extract indices of interest which, , extract rows of interest dataframe.
indices <- which(df$v1[1:(nrow(df)-1)]==df$v1[2:nrow(df)]) df.new <- df[indices,]
Comments
Post a Comment