Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...