![](/img/trans.png)
[英]R / dplyr : How to use filter functions in conjunction with the pipelining %>% operator?
[英]Using dplyr::filter with multiple conditions in conjunction with the pipe operator in R
如何與R中的管道%>%運算符一起使用具有多個條件的過濾器? 例如:
x <- rep(c(2011:2012),4)
y <- sort(rep(c(1:4),2))
qtr <- as.data.frame(cbind(x,y))
names(qtr) <- c("year","qtr")
現在,我需要過濾2012年第3季度的記錄,並在以下步驟中使用使用管道運算符。
z <- dplyr::filter(qtr,qtr$year==2012 & qtr$qtr==3)
以你的例子
x <- rep(c(2011:2012),4)
y <- sort(rep(c(1:4),2))
qtr <- as.data.frame(cbind(x,y))
names(qtr) <- c("year","qtr")
你可以做:
dplyr::filter(qtr,year==2012 & qtr==3)
#> year qtr
#> 1 2012 3
dplyr::filter(qtr, year==2012, qtr==3)
#> year qtr
#> 1 2012 3
管道:
qtr %>%
filter(year == 2012, qtr == 3)
#> year qtr
#> 1 2012 3
或逐步
qtr %>%
filter(year == 2012) %>%
filter(qtr == 3)
#> year qtr
#> 1 2012 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.