Reputation: 97
I am a newbie to R. Can someone help me with this
For below df, I need to check only the first row of each Item and if column "value = 0" then only I need to keep both the rows for that Item
df
Item city price value
A Delhi 10 0
A Mumbai 5 2
B Delhi 10 1
B Mumbai 5 2
Here for Item A, we will check the first row and value =0 so we will keep both the rows of Item A. But when we check the first row of Item B there value=1 so we remove both the rows of Item B.
output
Item city price value
A Delhi 10 0
A Mumbai 5 2
Upvotes: 1
Views: 47
Reputation: 886938
We can group by 'Item', and filter
the groups having the 'first' 'value' as 0
library(dplyr)
df %>%
group_by(Item) %>%
filter(first(value) == 0)
# A tibble: 2 x 4
# Groups: Item [1]
# Item city price value
# <chr> <chr> <int> <int>
#1 A Delhi 10 0
#2 A Mumbai 5 2
Or in base R
subset(df, Item %in% subset(df, !duplicated(Item) & !value)$Item)
df <- structure(list(Item = c("A", "A", "B", "B"), city = c("Delhi",
"Mumbai", "Delhi", "Mumbai"), price = c(10L, 5L, 10L, 5L), value = c(0L,
2L, 1L, 2L)), class = "data.frame", row.names = c(NA, -4L))
Upvotes: 2
Reputation: 388807
In base R, we can use ave
df[with(df, ave(value == 0, Item, FUN = function(x) x[1L])), ]
# Item city price value
#1 A Delhi 10 0
#2 A Mumbai 5 2
Upvotes: 1