2017-11-17 4 views
0

큰 데이터 프레임 (104029 x 142)이 있습니다.다중 열 패턴에 의한 효율적인 필터 행

멀티 특정 열 이름으로 value>0 인 행을 필터링하고 싶습니다.

df 
     word abrasive abrasives abrasivefree abrasion slurry solute solution .... 
1 composition  -0.2  0.2   -0.3 -0.40 0.2  0.1   0.20 .... 
2  ceria  0.1  0.2   -0.4 -0.20 -0.1  -0.2   0.20 .... 
3  diamond  0.3  -0.5   -0.6 -0.10 -0.1  -0.2  -0.15 .... 
4  acid  -0.1  -0.1   -0.2 -0.15 0.1  0.3   0.20 .... 
.... 

이제는 filter() 기능을 사용해 보았습니다. 괜찮습니다.

하지만이 방법은 효율적이지 않습니다.

각 열 이름을 정의해야하기 때문에 프로세스를 유지해야 할 때 힘든 작업이 필요합니다.

column_names <- c("agent", "agents", "liquid", "liquids", "slurry", 
        "solute", "solutes", "solution", "solutions") 

df_filter <- filter(df, agents>0 | agents>0 | liquid>0 | liquids>0 | slurry>0 | solute>0 | 
        solutes>0 | solution>0 | solutions>0) 

df_filter 
     word abrasive abrasives abrasivefree abrasion slurry solute solution .... 
1 composition  -0.2  0.2   -0.3 -0.40 0.2  0.1   0.20 .... 
2  ceria  0.1  0.2   -0.4 -0.20 -0.1  -0.2   0.20 .... 
4  acid  -0.1  -0.1   -0.2 -0.15 0.1  0.3   0.20 .... 
.... 

더 효율적인 방법이 있습니까?

+1

당신이에서 filter_if''보고'filter_at'을 가져야한다는'와우 ~이 작품의 –

답변

1

사용 dplyr::filter_at() :

library(dplyr) 

df_filter <- df %>% 
    filter_at(
     # select all the columns that are in your column_names vector 
     vars(one_of(column_names)) 
     # if any of those variables are greater than zero, keep the row 
     , any_vars(. > 0) 
    ) 
2

이 줄은 당신이 그럼 당신은 내가 dplyr에서 더 좋은 뭔가가 확신

df[filter_condition, ] 

을 사용할 수 있습니다

filter_condition <- apply(df[ , column_names], 1, function(x){sum(x>0)})>0 

을 테스트하는 조건에 대한 거짓 참/벡터를 반환합니다. 당신은 몇 가지 기능을 선택 select() 스타일 도우미를 사용할 수 있습니다

+0

dplyr'! 매우 효율적입니다. 빠른 답변에 감사드립니다. – Eva