使用时出错()

时间:2017-02-01 02:49:07

标签: r

对R来说很新......我跟随Crawley的The R Book中的一个例子。我从图书网站下载了一个数据集并将其加载到数据框

worms <- read.table("worms.txt", header=T)
attach(worms)
head(worms)

 Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
1     Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
2  Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
3   Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
4     Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
5 Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
6        Oak.Mead  3.1     2  Grassland     3.9 FALSE            2

现在,根据这本书,我应该可以输入

by(worms,Vegetation,mean)

获取按植被类型分组的输出,并具有数值变量(T / F强制为0/1)和NA为字符串变量的方法

参见R书中的第163页

但我得到了这个(为图像道歉但是blockquote并没有保留格式)

enter image description here

我做错了吗?这是本书使用的内容与现在的版本之间的版本问题吗?我完全糊涂了......

更新:以下是一些要求的输出

> str(worms)
'data.frame':   20 obs. of  7 variables:
 $ Field.Name  : Factor w/ 20 levels "Ashurst","Cheapside",..: 8 17 10 16 7 11 3 1 19 15 ...
 $ Area        : num  3.6 5.1 2.8 2.4 3.8 3.1 3.5 2.1 1.9 1.5 ...
 $ Slope       : num  11 2 3 5 0 2 3 0 0 4 ...
 $ Vegetation  : Factor w/ 5 levels "Arable","Grassland",..: 2 1 2 3 5 2 2 1 4 2 ...
 $ Soil.pH     : num  4.1 5.2 4.3 4.9 4.2 3.9 4.2 4.8 5.7 5 ...
 $ Damp        : logi  FALSE FALSE FALSE TRUE FALSE FALSE ...
 $ Worm.density: int  4 7 2 5 6 2 3 4 9 7 ...


> by(worms, Vegetation, summary)
Vegetation: Arable
          Field.Name      Area           Slope           Vegetation    Soil.pH         Damp        
 Ashurst       :1    Min.   :2.100   Min.   :0.000   Arable   :3    Min.   :4.500   Mode :logical  
 Pound.Hill    :1    1st Qu.:3.250   1st Qu.:1.000   Grassland:0    1st Qu.:4.650   FALSE:3        
 Silwood.Bottom:1    Median :4.400   Median :2.000   Meadow   :0    Median :4.800   NA's :0        
 Cheapside     :0    Mean   :3.867   Mean   :1.333   Orchard  :0    Mean   :4.833                  
 Church.Field  :0    3rd Qu.:4.750   3rd Qu.:2.000   Scrub    :0    3rd Qu.:5.000                  
 Farm.Wood     :0    Max.   :5.100   Max.   :2.000                  Max.   :5.200                  
 (Other)       :0                                                                                  
  Worm.density  
 Min.   :4.000  
 1st Qu.:4.500  
 Median :5.000  
 Mean   :5.333  
 3rd Qu.:6.000  
 Max.   :7.000  

--------------------------------------------------------------------------- 
Vegetation: Grassland
         Field.Name      Area           Slope            Vegetation    Soil.pH       Damp        
 Church.Field :1    Min.   :1.500   Min.   : 1.000   Arable   :0    Min.   :3.5   Mode :logical  
 Gravel.Pit   :1    1st Qu.:2.800   1st Qu.: 2.000   Grassland:9    1st Qu.:3.9   FALSE:8        
 Nashs.Field  :1    Median :3.100   Median : 3.000   Meadow   :0    Median :4.1   TRUE :1        
 North.Gravel :1    Mean   :2.911   Mean   : 3.667   Orchard  :0    Mean   :4.1   NA's :0        
 Nursery.Field:1    3rd Qu.:3.500   3rd Qu.: 4.000   Scrub    :0    3rd Qu.:4.2                  
 Oak.Mead     :1    Max.   :3.700   Max.   :11.000                  Max.   :5.0                  
 (Other)      :3                                                                                 
  Worm.density  
 Min.   :0.000  
 1st Qu.:1.000  
 Median :2.000  
 Mean   :2.444  
 3rd Qu.:3.000  
 Max.   :7.000  

--------------------------------------------------------------------------- 
Vegetation: Meadow
        Field.Name      Area           Slope           Vegetation    Soil.pH        Damp        
 Pond.Field  :1    Min.   :2.400   Min.   :0.000   Arable   :0    Min.   :4.900   Mode:logical  
 Rush.Meadow :1    1st Qu.:3.150   1st Qu.:0.000   Grassland:0    1st Qu.:4.900   TRUE:3        
 Water.Meadow:1    Median :3.900   Median :0.000   Meadow   :3    Median :4.900   NA's:0        
 Ashurst     :0    Mean   :3.467   Mean   :1.667   Orchard  :0    Mean   :4.933                 
 Cheapside   :0    3rd Qu.:4.000   3rd Qu.:2.500   Scrub    :0    3rd Qu.:4.950                 
 Church.Field:0    Max.   :4.100   Max.   :5.000                  Max.   :5.000                 
 (Other)     :0                                                                                 
  Worm.density  
 Min.   :5.000  
 1st Qu.:5.500  
 Median :6.000  
 Mean   :6.333  
 3rd Qu.:7.000  
 Max.   :8.000  

--------------------------------------------------------------------------- 
Vegetation: Orchard
        Field.Name      Area         Slope       Vegetation    Soil.pH       Damp        
 The.Orchard :1    Min.   :1.9   Min.   :0   Arable   :0    Min.   :5.7   Mode :logical  
 Ashurst     :0    1st Qu.:1.9   1st Qu.:0   Grassland:0    1st Qu.:5.7   FALSE:1        
 Cheapside   :0    Median :1.9   Median :0   Meadow   :0    Median :5.7   NA's :0        
 Church.Field:0    Mean   :1.9   Mean   :0   Orchard  :1    Mean   :5.7                  
 Farm.Wood   :0    3rd Qu.:1.9   3rd Qu.:0   Scrub    :0    3rd Qu.:5.7                  
 Garden.Wood :0    Max.   :1.9   Max.   :0                  Max.   :5.7                  
 (Other)     :0                                                                          
  Worm.density
 Min.   :9    
 1st Qu.:9    
 Median :9    
 Mean   :9    
 3rd Qu.:9    
 Max.   :9    

--------------------------------------------------------------------------- 
Vegetation: Scrub
           Field.Name      Area           Slope        Vegetation    Soil.pH         Damp        
 Cheapside      :1    Min.   :0.800   Min.   : 0   Arable   :0    Min.   :4.200   Mode :logical  
 Farm.Wood      :1    1st Qu.:1.850   1st Qu.: 6   Grassland:0    1st Qu.:4.575   FALSE:2        
 Garden.Wood    :1    Median :2.550   Median : 9   Meadow   :0    Median :4.900   TRUE :2        
 Gunness.Thicket:1    Mean   :2.425   Mean   : 7   Orchard  :0    Mean   :4.800   NA's :0        
 Ashurst        :0    3rd Qu.:3.125   3rd Qu.:10   Scrub    :4    3rd Qu.:5.125                  
 Church.Field   :0    Max.   :3.800   Max.   :10                  Max.   :5.200                  
 (Other)        :0                                                                               
  Worm.density 
 Min.   :3.00  
 1st Qu.:3.75  
 Median :5.00  
 Mean   :5.25  
 3rd Qu.:6.50  
 Max.   :8.00 

根据我应该看到的那本书:

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,您展示的页面之前的页面有attach(worms)命令,这就是您可以使用Vegetation而不是worms$Vegetation的原因。

可以重现的问题包括

worms <- read.table("http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/worms.txt", header = TRUE)
attach(worms)

自编写本书以来,by.data.frame的代码可能已发生变化,因为我确实会重现您的输出(不会按{{1}生成表格mean } type)但我可以使用

重现书中的内容
Vegetation
by(worms, Vegetation, function(x) sapply(x, mean))