如何在R中对表对象进行子集化?

jra*_*ara 7 r

如何根据值对表进行子集并返回这些值?这只返回索引:

with(chickwts, table(feed))
with(chickwts, table(feed)) > 11
which(with(chickwts, table(feed)) > 11)
Run Code Online (Sandbox Code Playgroud)

产量

> 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 
Run Code Online (Sandbox Code Playgroud)

joh*_*nes 6

这是另一种利用该Filter功能的方法:

Filter(function(x) x > 11, with(chickwts, table(feed)))
feed
   casein   linseed   soybean sunflower 
       12        12        14        12 
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 5

您需要使用计算值两次,因此使用中间变量很有用:

x <- with(chickwts, table(feed))
x[x>11]
feed
   casein   linseed   soybean sunflower 
       12        12        14        12 
Run Code Online (Sandbox Code Playgroud)