R Assignment Maths
R Assignment Maths
CHAPTER 4 -
DATA : DESCRIPTIVE STATISTICS AND TABULATION
1. Summary Commands
a.) str() command, gives information about the structure of the data.
for instance, the following data frame called grass. This contains two columns:
one is titled rich and relates to the number of plant species found in quadrats
and the other is titled graze and relates to the mowing treatment of the site:
> grass
rich graze
1 12 mow
2 15 mow
3 17 mow
4 11 mow
5 15 mow
6 8 unmow
7 9 unmow
8 7 unmow
9 9 unmow
b.) the summary() command is designed to give a quick statistical summary
of data objects. The output you get depends on the object you are looking
at.
> summary(grass)
rich graze
Min. : 7.00 mow :5
1st Qu.: 9.00 unmow:4
Median:11.00
Mean :11.44
3rd Qu.:15.00
Max. :17.00
c.) The summary() command works for both matrix and data frame objects
by summarizing the columns rather than the rows .
> grass.l $mow
[1] 12 15 17 11 15
$unmow
[1] 8 9 7 9
> summary(grass.l)
Length Class Mode
mow 5 -none- numeric
unmow 4 -none- numeric
2. Summary Statistics for Vectors
length(x) #(Gives the length of the vector and includes any NA values. The na.rm =
instruction does not work with this command.)
[1] 0.9574271
Altering Sample Length (length() command to alter the length of a vector by setting it to
a numeric value)
> unmow
[1] 8 9 7 9 NA
> length(unmow)
[1] 5
> length(unmow) = 4
> unmow
[1] 8 9 7 9
> length(unmow) = 6
> unmow
[1] 8 9 7 9 NA NA
> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> quantile(data2, 0.2) #(The quantile() command shows the quartiles by default; that is, the 0%,
25%, 50%, 75% and 100% quantiles.)
20%
357
[1] 3 5 7
> unmow
[1] 8 9 7 9 NA NA
> quantile(unmow)
Error in quantile.default(unmow) :
7.00 7.75 8.50 9.00 9.00 #(The quantile() command produces multiple results but you can alter
the default to produce quantiles for a single probability or several (in any order).
The fivenum() command produces a similar result to quantile(), but in this case 25% and
75% quantiles (the inter-quartiles) are replaced by the lower and upper hinge values.
These are similar to the quantiles and for samples with odd-number lengths they are the
same:
> dat
[1] 1 2 3 4 5 6
> quantile(dat)
> fivenum(dat)
3. Cumulative Statistics
a.) Simple Cumulative Commands
> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
[1] 3 8 15 20 23 25 31 39 44 50 59 63 68 75 78 82
[1] 3 5 7 7 7 7 7 8 8 8 9 9 9 9 9 9
[1] 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> seq(along = data2) #(The main purpose of the command is to generate sequences of values.)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> md = seq_along(data2)
> md [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
> md [1] 3 4 5 5 5 4 5 5 5 5 5 5 5 5 5 5
> rowMeans(fw)
> rowSums(fw)
11 28 20 11 28 49 53 81
> rowSums(mf)
[1] 274.25 262.15 215.75 240.95 227.95 228.75 197.85 264.75 247.95 262.35 267.35
[12] 264.35 259.05 245.85 229.75 247.45 275.35 253.05 201.25 295.05 275.55 176.85
> colMeans(mf)
> mean(mf)
The apply() command enables you to apply a function to rows or columns of a matrix or
data frame.
Blackbird 47 10 40 2 2
Chaffinch 19 3 5 0 2
Great Tit 50 0 10 7 0
House Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Song Thrush 4 0 6 0 0
> mean(bird[,2])
[1] 5.333333
> mean(bird[2,])
[1] 5.8
> colSums(bird)
175 32 69 13 6
> apply(bird,1,median)[1:2]
Blackbird Chaffinch
10 3
> apply(bird,1,median)[c(1,2,4)]
[1] 12 15 17 11 15
$unmow
[1] 8 9 7 9
> summary(grass.l)
> mean(grass.l$mow)
[1] 14
> max(grass.l$unmow)
[1] 9
$mow
[1] 14
$unmow
[1] 8.25
mow unmow
14.00 8.25
• SUMMARY TABLES
• MAKING CONTINGENCY TABLE
• creating contingency tables from vectors
> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> table(data2)
data2
2 3 4 5 6 7 8 9
1 3 2 4 2 2 1 1
> table(grass)
graze
rich mow unmow
7 0 1
8 0 1
9 0 2
11 1 0
12 1 0
15 2 0
17 1 0
> table(pw)
, , water = hi
plant
height sativa vulgaris
5 0 0
6 0 0
7 0 0
9 0 0
11 0 0
14 0 0
15 0 0
17 0 0
19 0 0
28 0 1
31 0 1
32 0 1
37 1 0
38 1 0
44 1 0
, , water = lo
plant
height sativa vulgaris
5 1 0
6 1 1
7 1 0
9 0 1
11 0 1
14 0 0
15 0 0
17 0 0
19 0 0
28 0 0
31 0 0
32 0 0
37 0 0
38 0 0
44 0 0
, , water = mid
plant
height sativa vulgaris
5 0 0
6 0 0
7 0 0
9 0 0
11 0 0
14 1 1
15 1 0
17 1 1
19 0 1
28 0 0
31 0 0
32 0 0
37 0 0
38 0 0
44 0 0
> table(height,water)
Error: object 'height' not found
> detach(pw)
hi lo mid
5 0 1 0
6 0 2 0
7 0 1 0
9 0 1 0
11 0 1 0
14 0 0 2
15 0 0 1
17 0 0 2
19 0 0 1
28 1 0 0
31 1 0 0
32 1 0 0
37 1 0 0
38 1 0 0
44 1 0 0
> with(pw,table(height,water))
water
height hi lo mid
5 0 1 0
6 0 2 0
7 0 1 0
9 0 1 0
11 0 1 0
14 0 0 2
15 0 0 1
17 0 0 2
19 0 0 1
28 1 0 0
31 1 0 0
32 1 0 0
37 1 0 0
38 1 0 0
44 1 0 0
> table(bird)
bird
0 2 3 4 5 6 7 8 9 10 16 19 40 46 47 50
9 4 2 2 1 1 1 1 1 2 1 1 1 1 1 1
> table(bird[,3],bird[,4],dnn=c('gdn','hedge')) #selecting columns in a ma
trix
hedge
gdn 0 2 4 7
0 1 0 0 0
5 1 0 0 0
6 1 0 0 0
8 0 0 1 0
10 0 0 0 1
40 0 1 0 0
Torridge
Taw 3 25
2 1 0
9 0 1
> fw.mat=as.matrix(fw)
> table(fw.mat[1,],fw.mat[4,],dnn=c('Taw','Exe'))
Exe
Taw 2 9
2 0 1
9 1 0
> pw.tab=with(pw,table(height,water))
> pw.tab
water
height hi lo mid
5 0 1 0
6 0 2 0
7 0 1 0
9 0 1 0
11 0 1 0
14 0 0 2
15 0 0 1
17 0 0 2
19 0 0 1
28 1 0 0
31 1 0 0
32 1 0 0
37 1 0 0
38 1 0 0
44 1 0 0
> pw.tab[1:3,c('mid','hi')]
water
height mid hi
5 0 0
6 0 0
7 0 0
> pw.tab[1:3,c(2,3)] #first three rows of 2 and 3rd column
water
height lo mid
5 1 0
6 2 0
7 1 0
> pw.tab[,c('hi',3)] #get error bcz numbers and name mix together
Error in `[.default`(pw.tab, , c("hi", 3)) : subscript out of bounds
> length(pw.tab) #length of table object
[1] 45
$unmow
[1] 8 9 7 9 NA
> gr.tab=as.table(as.matrix(stack(grass.1)))
#FOA convert list into data frame then into matrix after that convert int
o table
> colnames(gr.tab)=c('spp','graze') # to give column names
> gr.tab
spp graze
A 12 mow
B 15 mow
C 17 mow
D 11 mow
E 15 mow
F 8 unmow
G 9 unmow
H 7 unmow
I 9 unmow
J unmow
> is.table(gr.tab)
[1] TRUE
> class(gr.tab)
[1] "table"
> ftable(water~height+plant,data=pw)
water hi lo mid
height plant
5 sativa 0 1 0
vulgaris 0 0 0
6 sativa 0 1 0
vulgaris 0 1 0
7 sativa 0 1 0
vulgaris 0 0 0
9 sativa 0 0 0
vulgaris 0 1 0
11 sativa 0 0 0
vulgaris 0 1 0
14 sativa 0 0 1
vulgaris 0 0 1
15 sativa 0 0 1
vulgaris 0 0 0
17 sativa 0 0 1
vulgaris 0 0 1
19 sativa 0 0 0
vulgaris 0 0 1
28 sativa 0 0 0
vulgaris 1 0 0
31 sativa 0 0 0
vulgaris 1 0 0
32 sativa 0 0 0
vulgaris 1 0 0
37 sativa 1 0 0
vulgaris 0 0 0
38 sativa 1 0 0
vulgaris 0 0 0
44 sativa 1 0 0
vulgaris 0 0 0
> ftable(height~water+plant,data=pw)
height 5 6 7 9 11 14 15 17 19 28 31 32 37 38 44
water plant
hi sativa 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
vulgaris 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
lo sativa 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
vulgaris 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0
mid sativa 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0
vulgaris 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0
> gr.sub=gr.t[1:3,]
> colnames(gr.sub)=c('sativa','vulgaris')
> rownames(gr.sub)=c('hi','lo','mid')
FALSE FALSE 5 5
TRUE 3 3
TRUE FALSE 1 1
TRUE 0 0
• cross tabulation
> birds
Species Habitat Qty
1 Blackbird Garden 47
2 Chaffinch Garden 19
3 Great Tit Garden 50
4 House Sparrow Garden 46
5 Robin Garden 9
6 Song Thrush Garden 4
7 Blackbird Parkland 40
8 Chaffinch Parkland 5
9 Great Tit Parkland 10
10 House Sparrow Parkland 8
11 Song Thrush Parkland 6
12 Blackbird Hedgerow 10
13 Chaffinch Hedgerow 3
14 House Sparrow Hedgerow 16
15 Robin Hedgerow 3
16 Blackbird Woodland 2
17 Chaffinch Woodland 2
18 Robin Woodland 2
19 Blackbird Pasture 2
20 Great Tit Pasture 7
21 House Sparrow Pasture 4
> with(birds,table(Habitat,Species,Qty))# multiple table for each value of
qty
, , Qty = 2
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
0
Pasture 1 0 0 0 0
0
Woodland 1 1 0 0 1
0
, , Qty = 3
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 0 1 0 0 1
0
Parkland 0 0 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 4
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
1
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
0
Pasture 0 0 0 1 0
0
Woodland 0 0 0 0 0
0
, , Qty = 5
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 1 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 6
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
1
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 7
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
0
Pasture 0 0 1 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 8
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 1 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 9
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 1
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 10
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 1 0 0 0 0
0
Parkland 0 0 1 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 16
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 0 0 0 1 0
0
Parkland 0 0 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 19
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 1 0 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 40
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 1 0 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 46
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 0 1 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 47
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 1 0 0 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
, , Qty = 50
Species
Habitat Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 0 0 1 0 0
0
Hedgerow 0 0 0 0 0
0
Parkland 0 0 0 0 0
0
Pasture 0 0 0 0 0
0
Woodland 0 0 0 0 0
0
> inherits(birds.t,'table')
[1] TRUE
> birds.td
Species Habitat Freq
1 Blackbird Garden 47
2 Chaffinch Garden 19
3 Great Tit Garden 50
4 House Sparrow Garden 46
5 Robin Garden 9
6 Song Thrush Garden 4
7 Blackbird Hedgerow 10
8 Chaffinch Hedgerow 3
9 Great Tit Hedgerow 0
10 House Sparrow Hedgerow 16
11 Robin Hedgerow 3
12 Song Thrush Hedgerow 0
13 Blackbird Parkland 40
14 Chaffinch Parkland 5
15 Great Tit Parkland 10
16 House Sparrow Parkland 8
17 Robin Parkland 0
18 Song Thrush Parkland 6
19 Blackbird Pasture 2
20 Chaffinch Pasture 0
21 Great Tit Pasture 7
22 House Sparrow Pasture 4
23 Robin Pasture 0
24 Song Thrush Pasture 0
25 Blackbird Woodland 2
26 Chaffinch Woodland 2
27 Great Tit Woodland 0
28 House Sparrow Woodland 0
29 Robin Woodland 2
30 Song Thrush Woodland 0
• switching class
> class(bird) #matrix
[1] "table"
> class(bird)='table' #change class of a matrix to table
> bird.tt=bird
> class(bird.tt)='table'
> bird.tt=as.data.frame(bird.tt)
> names(bird.tt)=c('species','Habitat','Qty')
> bird.tt=bird.tt[which(bird.tt$Qty>0),]
> bird.tt
species Habitat Qty
1 Blackbird Garden 47
2 Chaffinch Garden 19
3 Great Tit Garden 50
4 House Sparrow Garden 46
5 Robin Garden 9
6 Song Thrush Garden 4
7 Blackbird Hedgerow 10
8 Chaffinch Hedgerow 3
10 House Sparrow Hedgerow 16
11 Robin Hedgerow 3
13 Blackbird Parkland 40
14 Chaffinch Parkland 5
15 Great Tit Parkland 10
16 House Sparrow Parkland 8
18 Song Thrush Parkland 6
19 Blackbird Pasture 2
21 Great Tit Pasture 7
22 House Sparrow Pasture 4
25 Blackbird Woodland 2
26 Chaffinch Woodland 2
29 Robin Woodland 2