Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

r - How to filter (with dplyr) for all values of a group if variable limit is reached?

Here's the dummy data:

cases <- rep(1:5,times=2)
var1 <- as.numeric(c(450,100,250,999,200,500,980,10,700,1000))
var2 <- as.numeric(c(111,222,333,444,424,634,915,12,105,152))

maindata1 <- data.frame(cases,var1,var2)

df1 <-  maindata1 %>%
  filter(var1 >950) %>%
  distinct(cases) %>%
  select(cases)

table1 <- maindata1 %>%
  filter(cases == 2 | cases == 4 | cases == 5) %>%
  arrange(cases)

> table1
  cases var1 var2
1     2  100  222
2     2  980  915
3     4  999  444
4     4  700  105
5     5  200  424
6     5 1000  152

I'm trying to formulate a dataframe which contains all the data related to cases where var1 >950 so it would show every value of var1 for those cases (also those values which are <950) and all values of var2 and would drop all cases where var1 won't reach >950. Table1 produces the desired dataframe but I had to enter filtering conditions manually. Is there a way to use that df1$cases as a filtering condition for extracting the same dataframe as a result?

I'm new to R and trying to learn data manipulation mainly with dplyr because it's syntax is almost understandable for layman.. so if someone can offer a solution based on dplyr that would be fantastic, of course I'm willing to hear solutions based on other packages as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Filter by max(var1) in each group defined by cases:

maindata1 %>%
  group_by(cases) %>%
  filter(max(var1) > 950) %>%
  arrange(cases)

#   cases var1 var2
# 1     2  100  222
# 2     2  980  915
# 3     4  999  444
# 4     4  700  105
# 5     5  200  424
# 6     5 1000  152

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...