Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views27 pages

R Assignment Maths

The document provides an overview of descriptive statistics and tabulation techniques in R, including commands for summarizing data structures, calculating summary statistics for vectors, data frames, and matrices. It details various commands such as str(), summary(), max(), min(), and quantile(), along with examples of their usage. Additionally, it covers cumulative statistics and the creation of contingency tables from both simple and complex data sets.

Uploaded by

Anshul Vashisth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views27 pages

R Assignment Maths

The document provides an overview of descriptive statistics and tabulation techniques in R, including commands for summarizing data structures, calculating summary statistics for vectors, data frames, and matrices. It details various commands such as str(), summary(), max(), min(), and quantile(), along with examples of their usage. Additionally, it covers cumulative statistics and the creation of contingency tables from both simple and complex data sets.

Uploaded by

Anshul Vashisth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

MATHS ASSIGNMENT

CHAPTER 4 -
DATA : DESCRIPTIVE STATISTICS AND TABULATION

BSC PHYSICAL SCIENCE


WITH CHEMISTRY
DATE - 16-04-2024
1303-ANANYA KHANNA
351-SACHIN KUMAR
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

a.) Summary Commands With Single Value Results-


 max(x, na.rm = FALSE) #(Shows the maximum value. By default NA values are
not removed. A value of NA is considered the largest unless na.rm = TRUE is used.)

for ex- > data2 [1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4


> max(data2)
[1] 9
 min(x, na.rm = FALSE) #(Shows the minimum value in a vector. If there are NA values,
this returns a value of NA unless na.rm = TRUE is used.)

for ex- > min(data2)


[1] 2

 length(x) #(Gives the length of the vector and includes any NA values. The na.rm =
instruction does not work with this command.)

for ex- > length(data 2)


[1] 16

 mean(x, na.rm = FALSE) #(Shows the arithmetic mean)

for ex- > mean(data2)


[1] 5.125
 sd(x, na.rm = FALSE) #(Shows the standard deviation)

for ex- > sd(unmow)


[1] NA

> sd(unmow, na.rm = TRUE)

[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

b.) Summary Commands With Multiple Results-

> 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%

> quantile(data2, c(0.2, 0.5, 0.8))

20% 50% 80%

357

> quantile(data2, c(0.5, 0.75, 0.25))

50% 75% 25%

5.00 6.25 3.75

> quantile(data2, c(0.2, 0.5, 0.8), names = F)

[1] 3 5 7

> unmow

[1] 8 9 7 9 NA NA
> quantile(unmow)

Error in quantile.default(unmow) :

missing values and NaN's not allowed if 'na.rm' is FALSE

> quantile(unmow, na.rm = T)

0% 25% 50% 75% 100%

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)

0% 25% 50% 75% 100%

1.00 2.25 3.50 4.75 6.00

> fivenum(dat)

[1] 1.0 2.0 3.5 5.0 6.0

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

> cumsum(data2) #(The cumulative sum of a vector)

[1] 3 8 15 20 23 25 31 39 44 50 59 63 68 75 78 82

> cummax(data2) #(The cumulative maximum value)

[1] 3 5 7 7 7 7 7 8 8 8 9 9 9 9 9 9

> cummin(data2) #(The cumulative minimum value)

[1] 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2

> cumprod(data2) #(The cumulative product)

[1] 3 15 105 525 1575 3150

[7] 18900 151200 756000 4536000 40824000 163296000

[13] 816480000 5715360000 17146080000 68584320000


b.) Complex Cumulative Commands
> data2

[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

> cumsum(data2) / seq(along = data2)

[1] 3.000000 4.000000 5.000000 5.000000 4.600000 4.166667 4.428571 4.875000

[9] 4.888889 5.000000 5.363636 5.250000 5.230769 5.357143 5.200000 5.125000

> 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

> for(i in 1:length(md)) md[i] = median(data2[1:i])

> md [1] 3 4 5 5 5 4 5 5 5 5 5 5 5 5 5 5

4. Summary Statistics for Data Frames


Two summary commands are designed especially for row data—rowMeans() and rowSums():

> rowMeans(fw)

Taw Torridge Ouse Exe Lyn Brook Ditch Fal

5.5 14.0 10.0 5.5 14.0 24.5 26.5 40.5

> rowSums(fw)

Taw Torridge Ouse Exe Lyn Brook Ditch Fal

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

[23] 204.95 218.85 208.75


Corresponding colSums() and colMeans() commands function in the same manner. In the following
example you see the mean() and colMeans() commands compared:

> colMeans(mf)

len sp alg no3 bod


19.640 15.800 58.400 2.046 145.960

> mean(mf)

len sp alg no3 bod

19.640 15.800 58.400 2.046 145.960

 The apply() command enables you to apply a function to rows or columns of a matrix or
data frame.

> apply(fw, 1, mean, na.rm = TRUE)

Taw Torridge Ouse Exe Lyn Brook Ditch Fal

5.5 14.0 10.0 5.5 14.0 24.5 26.5 40.5

5.) Summary Statistics for Matrix Objects


> bird

Garden Hedgerow Parkland Pasture Woodland

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)

Garden Hedgerow Parkland Pasture Woodland

175 32 69 13 6

> apply(bird,1,median)[1:2]

Blackbird Chaffinch

10 3

> apply(bird,1,median)[c(1,2,4)]

Blackbird Chaffinch House Sparrow


10 3 8

6.) Summary Statistics for Lists


> 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

> mean(grass.l$mow)

[1] 14

> max(grass.l$unmow)

[1] 9

> lapply(grass.l, mean, na.rm = TRUE)

$mow

[1] 14

$unmow

[1] 8.25

> sapply(grass.l, mean, na.rm = TRUE)

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

> sort(data2)#to rearrange data in ascending order


[1] 2 3 3 3 4 4 5 5 5 5 6 6 7 7 8 9

> table(data2)
data2
2 3 4 5 6 7 8 9
1 3 2 4 2 2 1 1

> graze #character data


[1] "mow" "mow" "mow" "mow" "mow" "unmow" "unmow" "unmow" "unmow
"
> table(graze)
graze
mow unmow
5 4

• creating contingency tables from complicated data

> grass #data frame


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

> 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

> fw #two columns numeric data frame


count speed
Taw 9 2
Torridge 25 3
Ouse 15 5
Exe 2 9
Lyn 14 14
Brook 25 24
Ditch 24 29
Fal 47 34
> table(fw)
speed
count 2 3 5 9 14 24 29 34
2 0 0 0 1 0 0 0 0
9 1 0 0 0 0 0 0 0
14 0 0 0 0 1 0 0 0
15 0 0 1 0 0 0 0 0
24 0 0 0 0 0 0 1 0
25 0 1 0 0 0 1 0 0
47 0 0 0 0 0 0 0 1
> pw #three column data frame
height plant water
1 9 vulgaris lo
2 11 vulgaris lo
3 6 vulgaris lo
4 14 vulgaris mid
5 17 vulgaris mid
6 19 vulgaris mid
7 28 vulgaris hi
8 31 vulgaris hi
9 32 vulgaris hi
10 7 sativa lo
11 6 sativa lo
12 5 sativa lo
13 14 sativa mid
14 17 sativa mid
15 15 sativa mid
16 44 sativa hi
17 38 sativa hi
18 37 sativa hi

> 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

• creating custom contingency tables


• selecting columns to use in a contingency table

> table(height,water)
Error: object 'height' not found

> attach(pw) #by using attach commamnd


> 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

> detach(pw)

> table(pw$height,pw$water) #by using $ sign to extract column

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(pw$height,pw$water,dnn=c('height','water')) # to give names to the


column
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

> 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

• creating contingency tables from matrix objects


> bird
Garden Hedgerow Parkland Pasture Woodland
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

> 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

> table(bird[3,],bird[4,],dnn=c('gdn','hedge')) #selecting rows in a matri


x
hedge
gdn 0 4 8 16 46
0 1 0 0 1 0
7 0 1 0 0 0
10 0 0 1 0 0
50 0 0 0 0 1

> with(as.data.frame(bird),table(Garden,Pasture)) #to use with command con


vert matrix into data frame

• using Rows of a data frame in a contingency table


> fw
count speed
Taw 9 2
Torridge 25 3
Ouse 15 5
Exe 2 9
Lyn 14 14
Brook 25 24
Ditch 24 29
Fal 47 34

> table(as.matrix(fw)[1,],as.matrix(fw)[2,],dnn = c('Taw','Torridge'))

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

• rotating data frames


> t(fw) #rows become colume and vice versa
Taw Torridge Ouse Exe Lyn Brook Ditch Fal
count 9 25 15 2 14 25 24 47
speed 2 3 5 9 14 24 29 34
> table(t(fw)[,1],t(fw)[,2],dnn=c('Taw','Torridge'))
Torridge
Taw 3 25
2 1 0
9 0 1
> class(t(fw))
[1] "matrix" "array"

• rotating matrix object


> t(bird)
Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
Garden 47 19 50 46 9 4
Hedgerow 10 3 0 16 3 0
Parkland 40 5 10 8 0 6
Pasture 2 0 7 4 0 0
Woodland 2 2 0 0 2 0
> class(t(bird))
[1] "table"

• SELECTING PARTS OF A TABLE OBJECTS


> #TRY IT OUT

> 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

> str(pw.tab) #to get table object structure


'table' int [1:15, 1:3] 0 0 0 0 0 0 0 0 0 1 ...
- attr(*, "dimnames")=List of 2
..$ height: chr [1:15] "5" "6" "7" "9" ...
..$ water : chr [1:3] "hi" "lo" "mid"

> pw.tab[1:3,] # display first three rows


water
height hi lo mid
5 0 1 0
6 0 2 0
7 0 1 0

> pw.tab[1:3,1] # first three rows of first column


5 6 7
0 0 0

> pw.tab[1:3,1:2] #first three rows of first and second column


water
height hi lo
5 0 1
6 0 2
7 0 1
> pw.tab[,'hi'] #display column labelled (hi)
5 6 7 9 11 14 15 17 19 28 31 32 37 38 44
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
1
> pw.tab[1:3,c('hi','mid')] #display first three rows of two columns
water
height hi mid
5 0 0
6 0 0
7 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

• CONVERTING AN OBJECT INTO A TABLE


> as.table(mf) #get an error
Error in as.table.default(mf) : cannot coerce to a table

> as.table(as.matrix(mf)) #data frame have to convert into matrix before c


onvert into table object
Length Speed Algae NO3 BOD
A 20.00 12.00 40.00 2.25 200.00
B 21.00 14.00 45.00 2.15 180.00
C 22.00 12.00 45.00 1.75 135.00
D 23.00 16.00 80.00 1.95 120.00
E 21.00 20.00 75.00 1.95 110.00
F 20.00 21.00 65.00 2.75 120.00
G 19.00 17.00 65.00 1.85 95.00
H 16.00 14.00 65.00 1.75 168.00
I 15.00 16.00 35.00 1.95 180.00
J 14.00 21.00 30.00 2.35 195.00
K 21.00 21.00 65.00 2.35 158.00
L 21.00 26.00 70.00 2.35 145.00
M 21.00 11.00 85.00 2.05 140.00
N 20.00 9.00 70.00 1.85 145.00
O 19.00 9.00 35.00 1.75 165.00
P 18.00 11.00 30.00 1.45 187.00
Q 17.00 17.00 50.00 1.35 190.00
R 19.00 15.00 60.00 2.05 157.00
S 21.00 19.00 70.00 1.25 90.00
T 13.00 21.00 25.00 1.05 235.00
U 16.00 22.00 35.00 2.55 200.00
V 25.00 9.00 85.00 2.85 55.00
W 24.00 11.00 80.00 2.95 87.00
X 23.00 16.00 80.00 2.85 97.00
Y 22.00 15.00 75.00 1.75 95.00

> grass.1 #a list


$mow
[1] 12 15 17 11 15

$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

TESTING FOE TABLE OBJECTS


> is.table(bird)
[1] TRUE

> is.table(gr.tab)
[1] TRUE

> class(gr.tab)
[1] "table"

> if(class(gr.tab)=='table')TRUE else FALSE #logical test


[1] TRUE

• COMPLEX (FLAT) TABLES


• making "flat" contingency tables
> ftable(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(col.items~row.items,data=data.object) tilde character basic syntax

• CREATE "FLAT" CONTINGENCY TABLES FROM COMPLEX DATA


> #TRY IT OUT

> with(pw,ftable(height,plant,water)) #table form in same order we specify col.


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

> with(pw,ftable(height,water,plant)) #specify col. in a new order


plant sativa vulgaris
height water
5 hi 0 0
lo 1 0
mid 0 0
6 hi 0 0
lo 1 1
mid 0 0
7 hi 0 0
lo 1 0
mid 0 0
9 hi 0 0
lo 0 1
mid 0 0
11 hi 0 0
lo 0 1
mid 0 0
14 hi 0 0
lo 0 0
mid 1 1
15 hi 0 0
lo 0 0
mid 1 0
17 hi 0 0
lo 0 0
mid 1 1
19 hi 0 0
lo 0 0
mid 0 1
28 hi 0 1
lo 0 0
mid 0 0
31 hi 0 1
lo 0 0
mid 0 0
32 hi 0 1
lo 0 0
mid 0 0
37 hi 1 0
lo 0 0
mid 0 0
38 hi 1 0
lo 0 0
mid 0 0
44 hi 1 0
lo 0 0
mid 0 0
> ftable(plant~height+water,data=pw) # here plant treates as col. and height+water as
plant sativa vulgaris
height water
5 hi 0 0
lo 1 0
mid 0 0
6 hi 0 0
lo 1 1
mid 0 0
7 hi 0 0
lo 1 0
mid 0 0
9 hi 0 0
lo 0 1
mid 0 0
11 hi 0 0
lo 0 1
mid 0 0
14 hi 0 0
lo 0 0
mid 1 1
15 hi 0 0
lo 0 0
mid 1 0
17 hi 0 0
lo 0 0
mid 1 1
19 hi 0 0
lo 0 0
mid 0 1
28 hi 0 1
lo 0 0
mid 0 0
31 hi 0 1
lo 0 0
mid 0 0
32 hi 0 1
lo 0 0
mid 0 0
37 hi 1 0
lo 0 0
mid 0 0
38 hi 1 0
lo 0 0
mid 0 0
44 hi 1 0
lo 0 0
mid 0 0

> 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

• selecting parts of ftable object


> gr.t=ftable(plant~height+water,data=pw)

> gr.t[1:3,] #display first three column


[,1] [,2]
[1,] 0 0
[2,] 1 0
[3,] 0 0

> gr.sub=gr.t[1:3,]

> colnames(gr.sub)=c('sativa','vulgaris')
> rownames(gr.sub)=c('hi','lo','mid')

> gr.sub=matrix(gr.t[1:3,],ncol = 2,dimnames = list(c('hi','lo','mid'),c('sativa','vul


> gr.sub #makes matrix from the parts of table object
sativa vulgaris
hi 0 0
lo 1 0
mid 0 0

• MAKING SELECTIVE "FLAT" CONTINGENCY TABLES


• CREATING SELECTIVE "FLAT" CONTINGENCY TABLES
> with(pw,ftable(height==14,water,plant)) #ftable with conditional column
plant sativa vulgaris
water
FALSE hi 3 3
lo 3 3
mid 2 2
TRUE hi 0 0
lo 0 0
mid 1 1

> with(pw,ftable(height==14,water=='hi',plant)) #ftable with two condition


al column
plant sativa vulgaris

FALSE FALSE 5 5
TRUE 3 3
TRUE FALSE 1 1
TRUE 0 0

> pw.t=pw[which(pw$height==14),] #form new data object


> pw.t
height plant water
4 14 vulgaris mid
13 14 sativa mid

> with(pw.t,ftable(height,plant,water)) #form ftable from new data object


water hi lo mid
height plant
14 sativa 0 0 1
vulgaris 0 0 1

• Testing "flat' table objects


> class(gr.t)
[1] "ftable"

> if(class(gr.t)=='ftable')TRUE else FALSE


[1] TRUE

• summary commands for tables


• carrying out summary commands on a contingency table
> bird #matrix object similar to table object
Garden Hedgerow Parkland Pasture Woodland
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

> rowSums(bird) # sum of rows


Blackbird Chaffinch Great Tit House Sparrow Robin So
ng Thrush
101 29 67 74 14
10

> colSums(bird) # sum of columns


Garden Hedgerow Parkland Pasture Woodland
175 32 69 13 6

> apply(bird,2,sum) #using apply command for sum of columns


Garden Hedgerow Parkland Pasture Woodland
175 32 69 13 6

> margin.table(bird)#overall sum


[1] 295
> margin.table(bird,1)# total sum of rows
Blackbird Chaffinch Great Tit House Sparrow Robin So
ng Thrush
101 29 67 74 14
10

> margin.table(bird,2)#total sum of column


Garden Hedgerow Parkland Pasture Woodland
175 32 69 13 6
> prop.table(bird)# proportion of total sum
Garden Hedgerow Parkland Pasture Woodland
Blackbird 0.159322034 0.033898305 0.135593220 0.006779661 0.006779661
Chaffinch 0.064406780 0.010169492 0.016949153 0.000000000 0.006779661
Great Tit 0.169491525 0.000000000 0.033898305 0.023728814 0.000000000
House Sparrow 0.155932203 0.054237288 0.027118644 0.013559322 0.000000000
Robin 0.030508475 0.010169492 0.000000000 0.000000000 0.006779661
Song Thrush 0.013559322 0.000000000 0.020338983 0.000000000 0.000000000

> prop.table(bird,1) # proportion of rows total


Garden Hedgerow Parkland Pasture Woodland
Blackbird 0.46534653 0.09900990 0.39603960 0.01980198 0.01980198
Chaffinch 0.65517241 0.10344828 0.17241379 0.00000000 0.06896552
Great Tit 0.74626866 0.00000000 0.14925373 0.10447761 0.00000000
House Sparrow 0.62162162 0.21621622 0.10810811 0.05405405 0.00000000
Robin 0.64285714 0.21428571 0.00000000 0.00000000 0.14285714
Song Thrush 0.40000000 0.00000000 0.60000000 0.00000000 0.00000000

> prop.table(bird,2) #proportion of col. total


Garden Hedgerow Parkland Pasture Woodland
Blackbird 0.26857143 0.31250000 0.57971014 0.15384615 0.33333333
Chaffinch 0.10857143 0.09375000 0.07246377 0.00000000 0.33333333
Great Tit 0.28571429 0.00000000 0.14492754 0.53846154 0.00000000
House Sparrow 0.26285714 0.50000000 0.11594203 0.30769231 0.00000000
Robin 0.05142857 0.09375000 0.00000000 0.00000000 0.33333333
Song Thrush 0.02285714 0.00000000 0.08695652 0.00000000 0.00000000

> addmargins(bird,1,mean) #row of mean values for the table


Garden Hedgerow Parkland Pasture Woodland
Blackbird 47.000000 10.000000 40.000000 2.000000 2.000000
Chaffinch 19.000000 3.000000 5.000000 0.000000 2.000000
Great Tit 50.000000 0.000000 10.000000 7.000000 0.000000
House Sparrow 46.000000 16.000000 8.000000 4.000000 0.000000
Robin 9.000000 3.000000 0.000000 0.000000 2.000000
Song Thrush 4.000000 0.000000 6.000000 0.000000 0.000000
mean 29.166667 5.333333 11.500000 2.166667 1.000000

> addmargins(bird,2,median) #column median for table


Garden Hedgerow Parkland Pasture Woodland median
Blackbird 47 10 40 2 2 10
Chaffinch 19 3 5 0 2 3
Great Tit 50 0 10 7 0 7
House Sparrow 46 16 8 4 0 8
Robin 9 3 0 0 2 2
Song Thrush 4 0 6 0 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

> #xtabs(freq.data~categories.list,data) #basic syntax


> birds.t=xtabs(Qty~Species+Habitat,data = birds) #here species form rows
and habitat form columns
> birds.t
Habitat
Species Garden Hedgerow Parkland Pasture Woodland
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

• testing cross-table(xtabs) object


> class(birds.t)
[1] "xtabs" "table"

> if(class(birds.t)=="xtabs")TRUE else FALSE #get an error


Error in if (class(birds.t) == "xtabs") TRUE else FALSE :
the condition has length > 1

> if(any(class(birds.t)=="xtabs"))TRUE else FALSE #any commands enables an


y of the elements in a vector
[1] TRUE

• A BETTER CLASS TEST


> is(birds.t,"xtabs")#another way to check objects
[1] TRUE

> inherits(birds.t,'table')
[1] TRUE

• Recreating original data from a contingency table


> birds.td=as.data.frame(birds.t)
> as.data.frame(birds.t,responseName = "Qty")
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
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

> 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

> birds.td=birds.td[which(birds.td$Freq>0),] #SELECT ROWS OF FREQUENCY GRE


ATER THAN 0
> 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
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

• 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

You might also like