9

値に基づいてテーブルをサブセット化し、それらの値を返すにはどうすればよいですか?これはインデックスのみを返します。

with(chickwts, table(feed))
with(chickwts, table(feed)) > 11
which(with(chickwts, table(feed)) > 11)

出力

> with(chickwts, table(feed))
feed
   casein horsebean   linseed  meatmeal   soybean sunflower 
       12        10        12        11        14        12 
> with(chickwts, table(feed)) > 11
feed
   casein horsebean   linseed  meatmeal   soybean sunflower 
     TRUE     FALSE      TRUE     FALSE      TRUE      TRUE 
> which(with(chickwts, table(feed)) > 11)
   casein   linseed   soybean sunflower 
        1         3         5         6 
4

3 に答える 3

6

Filter関数を利用する別のアプローチは次のとおりです。

Filter(function(x) x > 11, with(chickwts, table(feed)))
feed
   casein   linseed   soybean sunflower 
       12        12        14        12 
于 2012-10-19T11:34:41.870 に答える
6

計算値を2回使用する必要があるため、中間変数を使用すると便利です。

x <- with(chickwts, table(feed))
x[x>11]
feed
   casein   linseed   soybean sunflower 
       12        12        14        12 
于 2012-10-19T11:24:49.297 に答える
3

基本関数を使用する別のオプション:

subset(data.frame(table(chickwts$feed)), Freq > 11)

結果:

       Var1 Freq
1    casein   12
3   linseed   12
5   soybean   14
6 sunflower   12

dplyrパッケージの使用:

library(dplyr)
chickwts %>% 
  count(feed) %>%
  filter(n > 11) 

結果:

Source: local data frame [4 x 2]

       feed  n
1    casein 12
2   linseed 12
3   soybean 14
4 sunflower 12
于 2014-12-25T15:01:58.080 に答える