2017-09-26 73 views
1

我有一個數據框,我想通過某些標準的出現一定數量的過濾行。篩選器和計數沒有彙總數據幀

實施例:

yelp_tbl_asian %>% count(name) %>% filter(n > 10) 

# A tibble: 16 x 2 
       name  n 
       <chr> <int> 
1  China Buffet 12 
2  China Garden 12 
3  China House 14 
4   China Star 13 

在該步驟之後,我想篩選/由其他標準編輯數據幀。

實施例:

yelp_tbl_asian %>% filter(rating > 3.5) %>% select(attributes) %>% dmap(unlist) %>% count(attributes) %>% arrange(desc(n)) 

# A tibble: 227 x 2 
         attributes  n 
           <chr> <int> 
1   RestaurantsTakeOut: True 3265 
2  RestaurantsAttire: casual 3233 
3    GoodForKids: True 2986 
4 BusinessAcceptsCreditCards: True 2939 

的問題是,第一步驟是消除從數據集中以下標準。所以在第一次過濾之後,第二步就不可能了。我想結合這兩個命令/步驟。

+2

也許在filter()後面使用'left_join'將它與'name'上的原始df合併? – Sotos

回答

0

正如評論中所建議的,您可以繼續進行合併。我寧願避免這一點,並使用group_by + mutate組合避免你的表進行總結和簡單地增加每名計列(重複多次,有觀察):

yelp_tbl_asian %>% group_by(name) %>% mutate(n=n()) %>% filter(n > 10) 

然後你就可以繼續您的管道通過爲第二步添加更多的過濾器。