我對R中的dplyr函式不是很熟悉。然而,我想把我的資料集過濾成某些條件。
假設我的資料集中有超過100個屬性。我想用多個條件進行過濾。
我可以把我的編碼過濾列的位置,而不是他們的名字,如下:
我可以把我的編碼過濾列的位置,而不是他們的名字。
y = filter(retag, c(4: 50) ! = 8 & c(90。 110) == 8)
我已經嘗試了幾次類似的編碼,但是仍然沒有得到結果。
我也嘗試了如下的編碼,但不確定如何在rowSums函式中添加另一個條件。
retag[rowSums((retag! =8)[。 c(4: 50)])> =1,/span>]
我發現的唯一例子是使用資料集名稱而不是位置。
或者有什么方法可以使用資料集的位置進行過濾,因為我的資料相當龐大。
uj5u.com熱心網友回復:
你可以使用filter()/code>和across()/code>的組合。我沒有你的retag資料框架的版本,所以我創建了我自己的作為一個例子
set.seed(2000)
retag <- tibble()
col1 = runif(n = 1000。 min = 0。 max = 10) %>。 % round(0),
col2 = runif(n = 1000。 min = 0。 max = 10) %>。 % round(0),/span>
col3 = runif(n = 1000。 min = 0。 max = 10) %>。 % round(0),/span>
col4 = runif(n = 1000。 min = 0。 max = 10) %>。 % round(0),/span>
col5 = runif(n = 1000。 min = 0。 max = 10) %>。 % round(0)
)
# 過濾器,其中第一、第二和第三列都等于5,第四列不等于5。
retag %>%
過濾()
across(1: 3。 function(x) x == 5)。
across(4。 function(x) x ! = 5) x =!
)
uj5u.com熱心網友回復:
if_all()和if_any()最近被引入到tidyverse中,目的是在多個變數中進行過濾。
library(dplyr)
filter(retag, if_all( X: Y, ~ . x > 10 & 。 x < 35)).
# # A tibble: 5 x 2
# X Y
# <int> <int>
# 1 11 30
# 2 12 31
# 3 13 32 # 2 12 31
# 4 14 33 # 4 14 33
# 5 15 34
filter(retag, if_any(X: Y, ~ . x == 2 | 。 x == 25)) .
# # A tibble: 2 x 2
# X Y
# <int> <int>
# 1 2 21
# 2 6 25
資料
retag < -結構(list(X = 1。 20, Y = 20。 39),行。 names = c(NA。 -20L)。 class = c("tbl_df"。
"tbl", "data.frame"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/332222.html
標籤:
上一篇:在R中命名群組
