Reputation: 1
I have a dataset:
x y
j 5
k 2
b 9
b 1
k 1
g 8
h 2
k 4
I want to extract rows with value k in column x and row above it, so desired output is:
x y
j 5
k 2
b 1
k 1
h 2
k 4
How could I do that? I know how to extract rows with value k in column x:
df[df$x=="k",]
, but how to extract also a row above it?
Upvotes: 0
Views: 39
Reputation: 886938
One option with lead
library(dplyr)
df %>%
filter((x == 'k')|lead(x == 'k'))
# x y
#1 j 5
#2 k 2
#3 b 1
#4 k 1
#5 h 2
#6 k 4
Or with slice
df %>%
slice(which(x == 'k') + rep(c(-1, 0), sum(x == 'k')))
In base R
df[which(df$x == 'k') + rep(c(-1, 0), sum(df$x == 'k')),]
Upvotes: 1
Reputation: 2949
Extract the rownames and subset the data frame with the row names an row numbers as below
> df[sort(c(as.numeric(rownames(df[df$x=="k",]))-1, as.numeric(rownames(df[df$x=="k",])))),]
x y
1 j 5
2 k 2
4 b 1
5 k 1
7 h 2
8 k 4
Upvotes: 0
Reputation: 101024
You can try the code below using which
+ sort
indk <- which(df$x=="k")
df[sort(c(indk-1,indk)),]
which gives
x y
1 j 5
2 k 2
4 b 1
5 k 1
7 h 2
8 k 4
Data
> dput(df)
structure(list(x = c("j", "k", "b", "b", "k", "g", "h", "k"),
y = c(5L, 2L, 9L, 1L, 1L, 8L, 2L, 4L)), class = "data.frame", row.names = c(NA,
-8L))
Upvotes: 2