Reputation: 1848
This is an updated version of the problem:
Filtering a data frame according to values rank
I have a data frame(df) such as
group value x
a 4.2 2
a 4.5 2
a 6.2 2
b 5.1 3
b 3.5 3
a 4.2 2
a 5.1 2
b 6.4 3
b 3.3 3
b 4.1 3
a 5.0 2
The desired output is
group value
a 4.5
a 6.2
a 5.1
a 5.0
b 5.1
b 6.4
Namely, x assigns a value to each group.
desired output extracts the
The desired output includes all rows of df except related rows to these values. How can I do that with R? I will be vey glad for any help. Thanks a lot.
Upvotes: 0
Views: 80
Reputation: 92282
Here's another possible data.table
approach without reordering the data
library(data.table)
setDT(df)[, value[rank(value, ties.method = "first") > x[1L]], group]
# group V1
# 1: a 4.5
# 2: a 6.2
# 3: a 5.1
# 4: a 5.0
# 5: b 5.1
# 6: b 6.4
Or some type of base R approach
df$indx <- with(df, ave(value, group, FUN = rank, ties.method = "first"))
do.call(rbind, lapply(split(df, df$group), function(y) y[y$indx > unique(y$x), ]))
# group value x indx
# a.2 a 4.5 2 3
# a.3 a 6.2 2 6
# a.7 a 5.1 2 5
# a.11 a 5.0 2 4
# b.4 b 5.1 3 4
# b.8 b 6.4 3 5
Upvotes: 3
Reputation: 886988
You could try
library(dplyr)
df1 %>%
group_by(group) %>%
arrange(value) %>%
slice(-(1:x[1]))%>%
select(-x)
# group value
#1 a 4.5
#2 a 5.0
#3 a 5.1
#4 a 6.2
#5 b 5.1
#6 b 6.4
Or using base R
df2 <- df1[order(df1$group, df1$value),]
indx <- !!with(df2, ave(x, x, FUN=function(x) c(rep(0,x[1]),
rep(1, length(x)-x[1]))))
subset(df2, indx, select=-x)
Upvotes: 2