0% found this document useful (0 votes)
8 views92 pages

BPS21018 SEC Practical

Uploaded by

Garv
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)
8 views92 pages

BPS21018 SEC Practical

Uploaded by

Garv
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/ 92

NAME:- HARSH KUMAR JHA

R OLL NO:- BPS21094

COURSE:- PHYSICAL SCIENCE


WITH CHEMISTRY
SUBJECT:- MATH’S SEC
R version 4.3.2 (2023-10-31 ucrt) -- "Eye Holes"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.


Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> # CH-2 Starting Out: Becoming Familiar With R


> 3 + 9 + 12 - 17
[1] 7
> 12 + 17/2 - 3/4 * 2.5
[1] 18.625
> (12 + 17/2 - 3/4) * 2.5
[1] 49.375
> pi * 2^3 - sqrt(4)
[1] 23.13274
> abs(12 - 17* 2/3 -9)
[1] 8.333333
> factorial(4)
[1] 24
> log(2,10)
[1] 0.30103
> log(2, base = 10)
[1] 0.30103
> log10(2)
[1] 0.30103
> log(2)
[1] 0.6931472
> exp(0.6931472)
[1] 2
> 10^0.30103
[1] 2
> sin(45 * pi / 180)
[1] 0.7071068
> asin(0.7071068) * 180 / pi
[1] 45
> ans1 = 23+14/2-18+(7*pi/2)
> ans1
[1] 22.99557
> ans2 = 13+11+(17-4/7)
> ans1 + ans2/2
[1] 43.20986
> ans3 = ans2+9-2+pi
> ans4 <- 3+5
> ans5 <- ans1 * ans2
> ans3 + pi/ans4 -> ans6
> ans6
[1] 50.96286
> ans5
[1] 929.6782
> ans4
[1] 8
> ans3
[1] 50.57016
> ans2
[1] 40.42857
> ans1
[1] 22.99557
> data1 = c(3,5,7,5,3,2,6,8,5,6,9)
> data1
[1] 3 5 7 5 3 2 6 8 5 6 9
> data2 = c(data1,4,5,7,,3,4)
Error in c(data1, 4, 5, 7, , 3, 4) : argument 5 is empty
> data2 = c(data1, 4, 5, 7, 3, 4)
> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> data1 = c(6, 7, 6, 4, 8,data1)
> data1
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> day1 = c('mon','tue','wed','thu')
> day1
[1] "mon" "tue" "wed" "thu"
> day1 = c(day1,'fri')
> day1
[1] "mon" "tue" "wed" "thu" "fri"
> mix = c(data1,day1)
> mix
[1] "6" "7" "6" "4" "8" "3" "5" "7" "5" "3" "2"
"6" "8" "5" "6" "9" "mon" "tue" "wed" "thu" "fri"
> data3 = scan()
1: 6 7 8 7 6 3 8 9 10 7
11: 6 9
13:
Read 12 items
> data3
[1] 6 7 8 7 6 3 8 9 10 7 6 9
> day2 = scan(what = 'character')
1: Mon Tue Wed
4: Thu
5:
Read 4 items
> day2
[1] "Mon" "Tue" "Wed" "Thu"
> data4 = scan(sep = ',')
1: 11 22 33 44 55 66 77 88 99
2: 12 23 34 45 56 67 78 89 90
3:
Read 2 items
> data4
[1] 1.122334e+17 1.223345e+17
> data4 = scan(sep = ',')
1: 23,17,12.5,11,18,12,14.5,9
9: 11,9,12.5,14.5,17,8,21
16:
Read 15 items
>
> data4
[1] 23.0 17.0 12.5 11.0 18.0 12.0 14.5 9.0 11.0 9.0 12.5 14.5 17.0
8.0 21.0
> data5 = scan(sep = ',', what = 'char')
1: 'jan','feb','mar','apr','may','jun'
7: 'jul','aug','sep','oct','nov','dec'
13:
Read 12 items
> data5
[1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov"
"dec"
> getwd()
[1] "C:/Users/Gaurav/Documents"
> getwd()
[1] "C:/Users/Gaurav/Documents"
> setwd(desktop)
Error: object 'desktop' not found
> grass = read.csv(file.choose())
Error in file.choose() : file choice cancelled
> mow = c(12,15,17,11,15)
> unmow = c(8,9,7,9)
> grass = c(mow,unmow)
> grass
[1] 12 15 17 11 15 8 9 7 9
> grass = data.frame(mow,unmow)
Error in data.frame(mow, unmow) :
arguments imply differing number of rows: 5, 4
> ls()
[1] "ans1" "ans2" "ans3" "ans4" "ans5" "ans6" "data1" "data2"
"data3" "data4" "data5" "day1" "day2" "grass" "mix" "mow" "sach"
"unmow" "x"
> ls(pattern = 'a')
[1] "ans1" "ans2" "ans3" "ans4" "ans5" "ans6" "data1" "data2"
"data3" "data4" "data5" "day1" "day2" "grass" "sach"
> ls(pattern = 'ans')
[1] "ans1" "ans2" "ans3" "ans4" "ans5" "ans6"
> ls(pattern= '^a)
+ '

+ > > ls(pattern = '^a')


[1] "ans1" "ans2" "ans3" "ans4" "ans5" "ans6"
> cut
function (x, ...)
UseMethod("cut")
<bytecode: 0x000000ccf1b35cd0>
<environment: namespace:base>
> cut =
c('mow','mow','mow','mow','unmow','unmow','unmow','unmow','unmow')
> cut
[1] "mow" "mow" "mow" "mow" "unmow" "unmow" "unmow" "unmow"
"unmow"
> cut = c(mow,mow,mow,mow,unmow,unmow,unmow,unmow,unmow)
> cut
[1] 12 15 17 11 15 12 15 17 11 15 12 15 17 11 15 12 15 17 11 15 8 9 7
9 8 9 7 9 8 9 7 9 8 9 7 9 8 9 7 9
> as.character(cut)
[1] "12" "15" "17" "11" "15" "12" "15" "17" "11" "15" "12" "15" "17"
"11" "15" "12" "15" "17" "11" "15" "8" "9" "7" "9" "8" "9" "7" "9"
"8" "9" "7" "9"
[33] "8" "9" "7" "9" "8" "9" "7" "9"
> as.factor(cut)
[1] 12 15 17 11 15 12 15 17 11 15 12 15 17 11 15 12 15 17 11 15 8 9 7
9 8 9 7 9 8 9 7 9 8 9 7 9 8 9 7 9
Levels: 7 8 9 11 12 15 17
> data7= as.integer(data1)
> data7
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> data7n = as.numeric(data7)
> data7n
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> data7c = as.character(data7)
> data7c
[1] "6" "7" "6" "4" "8" "3" "5" "7" "5" "3" "2" "6" "8" "5" "6" "9"
> data7nt = as.numeric(data7c)
> data7nt
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> mow = c(11,22,33,44,55)
> unmow = c(2,4,5,6,5)
> grass = data.frame(mow,unmow)
> grass
mow unmow
1 11 2
2 22 4
3 33 5
4 44 6
5 55 5
> bird = matrix(mow,unmow,day1)
Error in matrix(mow, unmow, day1) : non-numeric matrix extent
> str(grass)
'data.frame': 5 obs. of 2 variables:
$ mow : num 11 22 33 44 55
$ unmow: num 2 4 5 6 5
>
>
>
>
>
> # CH=3 Starting Out: Working With Objects
> data1
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> data1[1]
[1] 6
> data1[3]
[1] 6
> data1[1:3]
[1] 6 7 6
> data1[-1]
[1] 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> data1[c(1,3,4,8)]
[1] 6 6 4 7
> data1[data1 >3]
[1] 6 7 6 4 8 5 7 5 6 8 5 6 9
> data1[data1 < 5 | data1 >7]
[1] 4 8 3 3 2 8 9
> length(data1)
[1] 16
> data1[(length(data1)-5) : length(data1)]
[1] 2 6 8 5 6 9
> data1
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> max(data1)
[1] 9
> which(data1 == max(data1))
[1] 16
> data1[seq(1, length(data1),2)]
[1] 6 6 8 5 5 2 8 6
> data5
[1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov"
"dec"
> data5[-1:-6]
[1] "jul" "aug" "sep" "oct" "nov" "dec"
> which(data5 == max(data5))
[1] 9
> unmow = c(8,9,7,9,NA)
> unmow
[1] 8 9 7 9 NA
> sort(unmow)
[1] 7 8 9 9
> sort(unmow,decreasing = TRUE)
[1] 9 9 8 7
> sort(unmow,un.last = NA)
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) :
unused argument (un.last = NA)
> sort(unmow, na.last = NA)
[1] 7 8 9 9
> sort(unmow, na.last = TRUE)
[1] 7 8 9 9 NA
> sort(unmow, na.last = FALSE)
[1] NA 7 8 9 9
> unmow
[1] 8 9 7 9 NA
> order(unmow)
[1] 3 1 2 4 5
> order(unmow, na.last = NA)
[1] 3 1 2 4
> order(unmow, na.last = FALSE)
[1] 5 3 1 2 4
> unmow
[1] 8 9 7 9 NA
> order(unmow)
[1] 3 1 2 4 5
> rank(unmow)
[1] 2.0 3.5 1.0 3.5 5.0
> unmow
[1] 8 9 7 9 NA
> rank(unmow, ties.method = 'first')
[1] 2 3 1 4 5
> rank(unmow, ties.method = 'average')
[1] 2.0 3.5 1.0 3.5 5.0
> rank(unmow, ties.method = 'max')
[1] 2 4 1 4 5
> rank(unmow, ties.method = 'random', na.last = 'keep')
[1] 2 3 1 4 NA
> dat.na
Error: object 'dat.na' not found
> dat.na
Error: object 'dat.na' not found
> dat.na = c(2,5,4,NA,7,3,,9,NA,12)
Error in c(2, 5, 4, NA, 7, 3, , 9, NA, 12) : argument 7 is empty
> data1
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> which(data1 = 6)
Error in which(data1 = 6) : unused argument (data1 = 6)
> which(data1 == 6)
[1] 1 3 12 15
> data1 == 6
[1] TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
TRUE FALSE FALSE TRUE FALSE
> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> data2 > 5
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE
FALSE FALSE TRUE FALSE FALSE
> data2 < 5
[1] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
TRUE FALSE FALSE TRUE TRUE
> data2 > 5 $ data2 < 8
Error: unexpected '<' in "data2 > 5 $ data2 <"
> data2 >5 & data2 <8
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
FALSE FALSE TRUE FALSE FALSE
> data8
Error: object 'data8' not found
> data7
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> data6
Error: object 'data6' not found
> data1 == 6 | data1 == 3
[1] TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
> length = c(20,21,22,23,21,20)
> speed = c(12,14,12,16,20,21)
> algae c(40,45,45,16,20,21)
Error: unexpected symbol in "algae c"
> algae = c(40,45,45,16,20,21)
> NO3 = c(2.25,2.14,1.33,1.66,1.78,2.45)
> BOD = c(200,180,135,120,110,120)
> mf = data.frame(length,speed,algae,NO3,BOD)
> mf
length speed algae NO3 BOD
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> mf[3,3]
[1] 45
> mf[3,1:4]
length speed algae NO3
3 22 12 45 1.33
> mf[,1]
[1] 20 21 22 23 21 20
> mf[c(1,3,5,7)]
Error in `[.data.frame`(mf, c(1, 3, 5, 7)) : undefined columns selected
> mf[c(1,3,5,7),]
length speed algae NO3 BOD
1 20 12 40 2.25 200
3 22 12 45 1.33 135
5 21 20 20 1.78 110
NA NA NA NA NA NA
> mf[c(1,3,5),-4]
length speed algae BOD
1 20 12 40 200
3 22 12 45 135
5 21 20 20 110
> mf[c(1,3,5),'algae']
[1] 40 45 20
> mf[3]
algae
1 40
2 45
3 45
4 16
5 20
6 21
> str(mf)
'data.frame': 6 obs. of 5 variables:
$ length: num 20 21 22 23 21 20
$ speed : num 12 14 12 16 20 21
$ algae : num 40 45 45 16 20 21
$ NO3 : num 2.25 2.14 1.33 1.66 1.78 2.45
$ BOD : num 200 180 135 120 110 120
> class(speed)
[1] "numeric"
> garden = c(47,19,50,46,9,4)
> garden
[1] 47 19 50 46 9 4
> cram = c('garden',''hedgerow','parkland','pasture','woodland')
Error: unexpected symbol in "cram = c('garden',''hedgerow"
> cram = c('garden','hedgerow','parkland','pasture','woodland')
> cram = c('garden','hedgerow','parkland','pasture','woodland')
> cnam = c('garden','hedgerow','parkland','pasture','woodland')
> rnam = c('blackbird','chaffinch','great tit','house
sparrow','robin','song thrush')
> bird =
matrix(c(47,19,50,46,9,4,10,3,0,16,3,0,40,5,10,8,0,6,2,0,7,4,0,0,2,2,0,0,
2,0),ncol = 5,dimnames = list(rnam,cnam))
> 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
> str(bird)
num [1:6, 1:5] 47 19 50 46 9 4 10 3 0 16 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:6] "blackbird" "chaffinch" "great tit" "house sparrow" ...
..$ : chr [1:5] "garden" "hedgerow" "parkland" "pasture" ...
> class(bird)
[1] "matrix" "array"
> bird[2,]
garden hedgerow parkland pasture woodland
19 3 5 0 2
> bird[,4]
blackbird chaffinch great tit house sparrow robin
song thrush
2 0 7 4 0
0
> bird[c('robin','blackbird'),]
garden hedgerow parkland pasture woodland
robin 9 3 0 0 2
blackbird 47 10 40 2 2
> bird[3,1]
[1] 50
> bird[4]
[1] 46
> sort(bird)
[1] 0 0 0 0 0 0 0 0 0 2 2 2 2 3 3 4 4 5 6 7 8 9 10
10 16 19 40 46 47 50
> order(bird)
[1] 9 12 17 20 23 24 27 28 30 19 25 26 29 8 11 6 22 14 18 21 16 5 7
15 10 2 13 4 1 3
> rank(bird)
[1] 29.0 26.0 30.0 28.0 22.0 16.5 23.5 14.5 5.0 25.0 14.5 5.0 27.0
18.0 23.5 21.0 5.0 19.0 11.5 5.0 20.0 16.5 5.0 5.0 11.5 11.5 5.0
5.0 11.5 5.0
> sort(bird[,1])
song thrush robin chaffinch house sparrow blackbird
great tit
4 9 19 46 47
50
> order(bird[,1])
[1] 6 5 2 4 1 3
> order(bird[,5])
[1] 3 4 6 1 2 5
> order(bird[,5],bird[,1])
[1] 6 4 3 5 2 1
> bird$garden
Error in bird$garden : $ operator is invalid for atomic vectors
> bird[,'garden']
blackbird chaffinch great tit house sparrow robin
song thrush
47 19 50 46 9
4
> bird['robin']
[1] NA
> bird['robin',]
garden hedgerow parkland pasture woodland
9 3 0 0 2
> mf[,NO3]
speed speed.1 length length.1 length.2 speed.2
1 12 12 20 20 20 12
2 14 14 21 21 21 14
3 12 12 22 22 22 12
4 16 16 23 23 23 16
5 20 20 21 21 21 20
6 21 21 20 20 20 21
> mf[,'NO3']
[1] 2.25 2.14 1.33 1.66 1.78 2.45
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
"package:grDevices" "package:utils" "package:datasets"
"package:methods" "Autoloads"
[9] "package:base"
> with(mf,algae)
[1] 40 45 45 16 20 21
> with(mf, sum(algae))
[1] 187
> head(mf)
length speed algae NO3 BOD
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> tail(mf)
length speed algae NO3 BOD
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> head(bird, n=3)
garden hedgerow parkland pasture woodland
blackbird 47 10 40 2 2
chaffinch 19 3 5 0 2
great tit 50 0 10 7 0
> summary(grass)
mow unmow
Min. :11 Min. :2.0
1st Qu.:22 1st Qu.:4.0
Median :33 Median :5.0
Mean :33 Mean :4.4
3rd Qu.:44 3rd Qu.:5.0
Max. :55 Max. :6.0
> summary(bird.m)
Error: object 'bird.m' not found
> summary(bird)
garden hedgerow parkland pasture
woodland
Min. : 4.00 Min. : 0.000 Min. : 0.00 Min. :0.000 Min.
:0
1st Qu.:11.50 1st Qu.: 0.750 1st Qu.: 5.25 1st Qu.:0.000 1st
Qu.:0
Median :32.50 Median : 3.000 Median : 7.00 Median :1.000 Median
:1
Mean :29.17 Mean : 5.333 Mean :11.50 Mean :2.167 Mean
:1
3rd Qu.:46.75 3rd Qu.: 8.250 3rd Qu.: 9.50 3rd Qu.:3.500 3rd
Qu.:2
Max. :50.00 Max. :16.000 Max. :40.00 Max. :7.000 Max.
:2
> summary(data3)
Min. 1st Qu. Median Mean 3rd Qu. Max.
3.000 6.000 7.000 7.167 8.250 10.000
> summary(data1)
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.000 4.750 6.000 5.625 7.000 9.000
> names(mf)
[1] "length" "speed" "algae" "NO3" "BOD"
> names(bird)
NULL
> row.names(mf)
[1] "1" "2" "3" "4" "5" "6"
> row.names(bird)
[1] "blackbird" "chaffinch" "great tit" "house sparrow"
"robin" "song thrush"
> colnames(mf)
[1] "length" "speed" "algae" "NO3" "BOD"
> colnames(bird)
[1] "garden" "hedgerow" "parkland" "pasture" "woodland"
> dimnames(mf)
[[1]]
[1] "1" "2" "3" "4" "5" "6"

[[2]]
[1] "length" "speed" "algae" "NO3" "BOD"

> dimnames(bird)
[[1]]
[1] "blackbird" "chaffinch" "great tit" "house sparrow"
"robin" "song thrush"

[[2]]
[1] "garden" "hedgerow" "parkland" "pasture" "woodland"

> names(mf)
[1] "length" "speed" "algae" "NO3" "BOD"
> names(mf) = c('len','sp','alg','no3','bod)
+ ')
> names(mf)
[1] "len" "sp" "alg" "no3" "bod)\n"
> species = c('Bbird','C.Finch','Gt.Tit','Sparrow','Robin','Thrush')
> habitats = c('Gdn','Hegde','Park','Field','Wood')
> dinames(bird) = list(species,habitats)
Error in dinames(bird) = list(species, habitats) :
could not find function "dinames<-"
> dimnames(bird) = list(species,habitats)
> bird
Gdn Hegde Park Field Wood
Bbird 47 10 40 2 2
C.Finch 19 3 5 0 2
Gt.Tit 50 0 10 7 0
Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Thrush 4 0 6 0 0
> bird.t = t(bird)
> bird.t
Bbird C.Finch Gt.Tit Sparrow Robin Thrush
Gdn 47 19 50 46 9 4
Hegde 10 3 0 16 3 0
Park 40 5 10 8 0 6
Field 2 0 7 4 0 0
Wood 2 2 0 0 2 0
> mow
[1] 11 22 33 44 55
> unmow
[1] 8 9 7 9 NA
> data1
[1] 6 7 6 4 8 3 5 7 5 3 2 6 8 5 6 9
> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> data4
[1] 23.0 17.0 12.5 11.0 18.0 12.0 14.5 9.0 11.0 9.0 12.5 14.5 17.0
8.0 21.0
> grass.list = list(mow,unmow,data1,data2,data4)
> grass.list
[[1]]
[1] 11 22 33 44 55

[[2]]
[1] 8 9 7 9 NA

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

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

[[5]]
[1] 23.0 17.0 12.5 11.0 18.0 12.0 14.5 9.0 11.0 9.0 12.5 14.5 17.0
8.0 21.0

> names(grass.list) = c('mow','unmow','data1','data2','data4


+
+ ')
> grass.list
$mow
[1] 11 22 33 44 55

$unmow
[1] 8 9 7 9 NA

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

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

$`data4\n\n`
[1] 23.0 17.0 12.5 11.0 18.0 12.0 14.5 9.0 11.0 9.0 12.5 14.5 17.0
8.0 21.0

> sample1 = c(5,6,9,12,8)


> sample2 = c(7,9,13,10,NA)
> sample1;sample2
[1] 5 6 9 12 8
[1] 7 9 13 10 NA
> my.frame = data.frame(sample1,sample2)
> my.frame
sample1 sample2
1 5 7
2 6 9
3 9 13
4 12 10
5 8 NA
> response = c(5,6,9,12,8,7,9,13,10)
> predictor = c(rep('open',5),rep('closed',4))
> response;predictor
[1] 5 6 9 12 8 7 9 13 10
[1] "open" "open" "open" "open" "open" "closed" "closed"
"closed" "closed"
> my.frame2 = data.frame(response,predictor)
> my.frame2
response predictor
1 5 open
2 6 open
3 9 open
4 12 open
5 8 open
6 7 closed
7 9 closed
8 13 closed
9 10 closed
> mow
[1] 11 22 33 44 55
> unkow
Error: object 'unkow' not found
> unmow
[1] 8 9 7 9 NA
> length(unmow)
[1] 5
> a= c(1,2,3,4,5)
> b = c(7,8,9,0)
> a;b
[1] 1 2 3 4 5
[1] 7 8 9 0
> length(b)
[1] 4
> length(b) = length(a)
> length(b)
[1] 5
>
R version 4.3.2 (2023-10-31 ucrt) -- "Eye Holes"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.


Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> sample1 = c(5,6,9,12,8)


> sample2 = c(7,9,13,10,NA)
> sample1;sample2
[1] 5 6 9 12 8
[1] 7 9 13 10 NA
> cmat = cbind(sample1,sample2)
> cmat
sample1 sample2
[1,] 5 7
[2,] 6 9
[3,] 9 13
[4,] 12 10
[5,] 8 NA
> rmat = rbind(sample1,sample2)
> rmat
[,1] [,2] [,3] [,4] [,5]
sample1 5 6 9 12 8
sample2 7 9 13 10 NA
> sample3 = c('a','b','c','d','e')
> sample3
[1] "a" "b" "c" "d" "e"
> mix.mat = cbind(sample1,sample2,sample3)
> mix.mat
sample1 sample2 sample3
[1,] "5" "7" "a"
[2,] "6" "9" "b"
[3,] "9" "13" "c"
[4,] "12" "10" "d"
[5,] "8" NA "e"
> str(mix.mat)
chr [1:5, 1:3] "5" "6" "9" "12" "8" "7" "9" "13" "10" NA "a" "b" "c" "d"
"e"
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "sample1" "sample2" "sample3"
> as.numeric(mix.mat)
[1] 5 6 9 12 8 7 9 13 10 NA NA NA NA NA NA
Warning message:
NAs introduced by coercion
> as.numeric(mix.mat[,1])
[1] 5 6 9 12 8
> mix.mat[,1] = as.numeric(mix.mat[,1])
> mix.mat
sample1 sample2 sample3
[1,] "5" "7" "a"
[2,] "6" "9" "b"
[3,] "9" "13" "c"
[4,] "12" "10" "d"
[5,] "8" NA "e"
> sample1;sample2
[1] 5 6 9 12 8
[1] 7 9 13 10 NA
> all.sample = c(sample1,sample2)
> all.sample
[1] 5 6 9 12 8 7 9 13 10 NA
> mat = matrix(all.sample, nrow = 2)
> mat
[,1] [,2] [,3] [,4] [,5]
[1,] 5 9 8 9 10
[2,] 6 12 7 13 NA
> mat = matrix(all.sample, ncol = 2)
> mat
[,1] [,2]
[1,] 5 7
[2,] 6 9
[3,] 9 13
[4,] 12 10
[5,] 8 NA
> cnam = c('sample1','sample2')
> rnam = c("site1','site2','site3','site4','site5')
+ mat
+ = matrix(all.sample, ncol=2, dimnames = list(rnam, cnam))

+ > mat
[,1] [,2]
[1,] 5 7
[2,] 6 9
[3,] 9 13
[4,] 12 10
[5,] 8 NA
> cram
[1] "garden" "hedgerow" "parkland" "pasture" "woodland"
> cnam = c('sample1','sample2')
> rnam = c("site1','site2','site3','site4','site5')

+ > mat = matrix(all.sample, ncol = 2, dimnames = list(rnam,cnam))


Error in matrix(all.sample, ncol = 2, dimnames = list(rnam, cnam)) :
length of 'dimnames' [1] not equal to array extent
> mat
[,1] [,2]
[1,] 5 7
[2,] 6 9
[3,] 9 13
[4,] 12 10
[5,] 8 NA
> mat = matrix(all.sample, ncol = 2, dimnames =
list(c('site1','site2','site3','site4','site5'),c('sample1','sample2')))
> mat
sample1 sample2
site1 5 7
site2 6 9
site3 9 13
site4 12 10
site5 8 NA
> bird
Gdn Hegde Park Field Wood
Bbird 47 10 40 2 2
C.Finch 19 3 5 0 2
Gt.Tit 50 0 10 7 0
Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Thrush 4 0 6 0 0
> ii = order(bird[,1],bird[,2],bird[,4])
> ii
[1] 6 5 2 4 1 3
> bird[ii,c(5:1)]
Wood Field Park Hegde Gdn
Thrush 0 0 6 0 4
Robin 2 0 0 3 9
C.Finch 2 0 5 3 19
Sparrow 0 4 8 16 46
Bbird 2 2 40 10 47
Gt.Tit 0 7 10 0 50
> urban = c(11,8,9,28,9,1)
> urban
[1] 11 8 9 28 9 1
> bird.extra
Error: object 'bird.extra' not found
> cbind(bird,urban)[ii,]
Gdn Hegde Park Field Wood urban
Thrush 4 0 6 0 0 1
Robin 9 3 0 0 2 9
C.Finch 19 3 5 0 2 8
Sparrow 46 16 8 4 0 28
Bbird 47 10 40 2 2 11
Gt.Tit 50 0 10 7 0 9
> matrix(c(bird,urban), ncol = 6)[ii,]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 4 0 6 0 0 1
[2,] 9 3 0 0 2 9
[3,] 19 3 5 0 2 8
[4,] 46 16 8 4 0 28
[5,] 47 10 40 2 2 11
[6,] 50 0 10 7 0 9
> ii = order(bird[3,],bird[4,],bird[1,])
> ii
[1] 5 2 4 3 1
> rbind(bird[ii,])
Gdn Hegde Park Field Wood
Robin 9 3 0 0 2
C.Finch 19 3 5 0 2
Sparrow 46 16 8 4 0
Gt.Tit 50 0 10 7 0
Bbird 47 10 40 2 2
> mat
sample1 sample2
site1 5 7
site2 6 9
site3 9 13
site4 12 10
site5 8 NA
> mat2frame = as.data.frame(mat)
> mat2frame
sample1 sample2
site1 5 7
site2 6 9
site3 9 13
site4 12 10
site5 8 NA
> str(mat)
num [1:5, 1:2] 5 6 9 12 8 7 9 13 10 NA
- attr(*, "dimnames")=List of 2
..$ : chr [1:5] "site1" "site2" "site3" "site4" ...
..$ : chr [1:2] "sample1" "sample2"
> str(mat2frame)
'data.frame': 5 obs. of 2 variables:
$ sample1: num 5 6 9 12 8
$ sample2: num 7 9 13 10 NA
> data5
[1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov"
"dec"
> yr.matrix = matrix(data5, ncol = 4, dimnames =
list(c('row1','row2','row3'),c('qtr1','qtr2','qtr3','qtr4')))
> yr.matrix
qtr1 qtr2 qtr3 qtr4
row1 "jan" "apr" "jul" "oct"
row2 "feb" "may" "aug" "nov"
row3 "mar" "jun" "sep" "dec"
> yr.frame = as.data.frame(yr.matrix)
> yr.frame
qtr1 qtr2 qtr3 qtr4
row1 jan apr jul oct
row2 feb may aug nov
row3 mar jun sep dec
> str(yr.matrix)
chr [1:3, 1:4] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep"
"oct" "nov" "dec"
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "row1" "row2" "row3"
..$ : chr [1:4] "qtr1" "qtr2" "qtr3" "qtr4"
> str(yr.frame)
'data.frame': 3 obs. of 4 variables:
$ qtr1: chr "jan" "feb" "mar"
$ qtr2: chr "apr" "may" "jun"
$ qtr3: chr "jul" "aug" "sep"
$ qtr4: chr "oct" "nov" "dec"
> yr.mat = as,matrix(yr.frame)
Error: unexpected ',' in "yr.mat = as,"
> yr.mat = as.matrix(yr.frame)
> yr.mat
qtr1 qtr2 qtr3 qtr4
row1 "jan" "apr" "jul" "oct"
row2 "feb" "may" "aug" "nov"
row3 "mar" "jun" "sep" "dec"
> str(yr.mat)
chr [1:3, 1:4] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep"
"oct" "nov" "dec"
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "row1" "row2" "row3"
..$ : chr [1:4] "qtr1" "qtr2" "qtr3" "qtr4"
> yr.mat[,1]
row1 row2 row3
"jan" "feb" "mar"
> mf
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> str(mf)
'data.frame': 6 obs. of 5 variables:
$ len : num 20 21 22 23 21 20
$ sp : num 12 14 12 16 20 21
$ alg : num 40 45 45 16 20 21
$ no3 : num 2.25 2.14 1.33 1.66 1.78 2.45
$ bod)
: num 200 180 135 120 110 120
> frame.list = as.list(mf)
> frame.list
$len
[1] 20 21 22 23 21 20

$sp
[1] 12 14 12 16 20 21

$alg
[1] 40 45 45 16 20 21
$no3
[1] 2.25 2.14 1.33 1.66 1.78 2.45

$`bod)\n`
[1] 200 180 135 120 110 120

> yr.list = as.list(as.data.frame(yr.matrix))


> yr.list
$qtr1
[1] "jan" "feb" "mar"

$qtr2
[1] "apr" "may" "jun"

$qtr3
[1] "jul" "aug" "sep"

$qtr4
[1] "oct" "nov" "dec"

> a.frame = data.frame(frame.list)


> a.frame
len sp alg no3 bod..
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> str(a.frame)
'data.frame': 6 obs. of 5 variables:
$ len : num 20 21 22 23 21 20
$ sp : num 12 14 12 16 20 21
$ alg : num 40 45 45 16 20 21
$ no3 : num 2.25 2.14 1.33 1.66 1.78 2.45
$ bod..: num 200 180 135 120 110 120
> a.matrix = as.matrix(data.frame(frame.list))
> a.matrix
len sp alg no3 bod..
[1,] 20 12 40 2.25 200
[2,] 21 14 45 2.14 180
[3,] 22 12 45 1.33 135
[4,] 23 16 16 1.66 120
[5,] 21 20 20 1.78 110
[6,] 20 21 21 2.45 120
> str(a.matrix)
num [1:6, 1:5] 20 21 22 23 21 20 12 14 12 16 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "len" "sp" "alg" "no3" ...
> algae = frame.list$alg
> algae
[1] 40 45 45 16 20 21
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> # CH=4 Data: Descriptive Statistics and Tabulation
> data1a = c(3,5,7,5,3,2,6,8,5,6,9,4,5,7,3,4
+ data1a
Error: unexpected symbol in:
"data1a = c(3,5,7,5,3,2,6,8,5,6,9,4,5,7,3,4
data1a"
> dt1 = c(3,5,7,5,3,2,6,8,5,6,9,4,5,7,3,4)
> dt1
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> mean(dt1)
[1] 5.125
> max(dt1)
[1] 9
> min(dt1)
[1] 2
> length(dt1)
[1] 16
> sd(dt1)
[1] 1.962142
> log(dt1)
[1] 1.0986123 1.6094379 1.9459101 1.6094379 1.0986123 0.6931472
1.7917595 2.0794415 1.6094379 1.7917595 2.1972246 1.3862944 1.6094379
1.9459101 1.0986123 1.3862944
> summary(dt1)
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.000 3.750 5.000 5.125 6.250 9.000
> quantile(dt1)
0% 25% 50% 75% 100%
2.00 3.75 5.00 6.25 9.00
> fivenum(dt1)
[1] 2.0 3.5 5.0 6.5 9.0
> quantile(dt1,0.2)
20%
3
> quantile(dt1,c(0.2,0.5,0.8))
20% 50% 80%
3 5 7
> quantile(dt1,c(0.5,0.75,0.25))
50% 75% 25%
5.00 6.25 3.75
> quantile(dt1, c(0.2,0.5,0.8),names = F)
[1] 3 5 7
> cumsum(dt1)
[1] 3 8 15 20 23 25 31 39 44 50 59 63 68 75 78 82
> cummax(dt1)
[1] 3 5 7 7 7 7 7 8 8 8 9 9 9 9 9 9
> cummin(dt1)
[1] 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2
> cumprod(dt1)
[1] 3 15 105 525 1575
3150 18900 151200 756000 4536000 40824000
163296000 816480000
[14] 5715360000 17146080000 68584320000
> data5
[1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov"
"dec"
> cummax(data5)
[1] NA NA NA NA NA NA NA NA NA NA NA NA
Warning message:
NAs introduced by coercion
> dt1
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> seq(along = dt1)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
> seq_along(dt1)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
> seq(from = 1, to = 10, by = 2)
[1] 1 3 5 7 9
> seq(1,10,2)
[1] 1 3 5 7 9
> seq(dt1)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
> cumsum(dt1)/seq(along = dt1)
[1] 3.000000 4.000000 5.000000 5.000000 4.600000 4.166667 4.428571
4.875000 4.888889 5.000000 5.363636 5.250000 5.230769 5.357143 5.200000
5.125000
> dt1
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> md = seq_along(dt1)
> 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(dt1[1:i])
Error: unexpected symbol in "for(i in 1:length(md) md"
> for(i in 1:length(md)) md[i] = median(dt1[1:i])
> ,md
Error: unexpected ',' in ","
> md
[1] 3 4 5 5 5 4 5 5 5 5 5 5 5 5 5 5
> md = seq_along(dt1)
> for(i in 1:length(md)) md[i] = sd(dt1[1:i])
> md
[1] NA 1.414214 2.000000 1.632993 1.673320 1.834848 1.812654
2.100170 1.964971 1.885618 2.157440 2.094365 2.006400 1.984833 2.007130
1.962142
> mf
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> rowMeans(mf)
[1] 54.850 52.428 43.066 35.332 34.556 36.890
> rowSums(mf)
[1] 274.25 262.14 215.33 176.66 172.78 184.45
> rowSums(mf)
[1] 274.25 262.14 215.33 176.66 172.78 184.45
> colMeans(mf)
len sp alg no3 bod)\n
21.16667 15.83333 31.16667 1.93500 144.16667
> mean(mf)
[1] NA
Warning message:
In mean.default(mf) : argument is not numeric or logical: returning NA
> mf
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> mean(mf)
[1] NA
Warning message:
In mean.default(mf) : argument is not numeric or logical: returning NA
> apply(mf, 1, median, na.rm = TRUE)
[1] 20 21 22 16 20 21
> bird
Gdn Hegde Park Field Wood
Bbird 47 10 40 2 2
C.Finch 19 3 5 0 2
Gt.Tit 50 0 10 7 0
Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Thrush 4 0 6 0 0
> mean(bird[,2])
[1] 5.333333
> mrean(bird[2,])
Error in mrean(bird[2, ]) : could not find function "mrean"
> mean(bird[2,])
[1] 5.8
> colSums(bird)
Gdn Hegde Park Field Wood
175 32 69 13 6
> rowMeans(bird)
Bbird C.Finch Gt.Tit Sparrow Robin Thrush
20.2 5.8 13.4 14.8 2.8 2.0
> apply(bird,1,median)
Bbird C.Finch Gt.Tit Sparrow Robin Thrush
10 3 7 8 2 0
> apply(bird,2,median)
Gdn Hegde Park Field Wood
32.5 3.0 7.0 1.0 1.0
> apply(bird,1,median)[1:2]
Bbird C.Finch
10 3
> apply(bird,1,median)[c(1,2,4)]
Bbird C.Finch Sparrow
10 3 8
> apply(bird,1,median)[c(1,2,'robin')]
<NA> <NA> <NA>
NA NA NA
> apply(bird,1,median)[c('Bbird','robin')]
Bbird <NA>
10 NA
> apply(bird,1,median)[c(1,2,'Robin')]
<NA> <NA> Robin
NA NA 2
> apply(bird,1,median)[c('Bbird','Robin')]
Bbird Robin
10 2
> sum(md)
[1] NA
> dt1
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> table(dt1)
dt1
2 3 4 5 6 7 8 9
1 3 2 4 2 2 1 1
> sort(dt1)
[1] 2 3 3 3 4 4 5 5 5 5 6 6 7 7 8 9
> grass
mow unmow
1 11 2
2 22 4
3 33 5
4 44 6
5 55 5
> table(grass)
unmow
mow 2 4 5 6
11 1 0 0 0
22 0 1 0 0
33 0 0 1 0
44 0 0 0 1
55 0 0 1 0
> bird
Gdn Hegde Park Field Wood
Bbird 47 10 40 2 2
C.Finch 19 3 5 0 2
Gt.Tit 50 0 10 7 0
Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
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[,1],bird[,2],dnn = c('Gdn','Hedge'))
Hedge
Gdn 0 3 10 16
4 1 0 0 0
9 0 1 0 0
19 0 1 0 0
46 0 0 0 1
47 0 0 1 0
50 1 0 0 0
> table(bird[3,],bird[1,],dnn = c('Gt.tit','BlackBird'))
BlackBird
Gt.tit 2 10 40 47
0 1 1 0 0
7 1 0 0 0
10 0 0 1 0
50 0 0 0 1
> with(as.data.frame(bird),table(Gdn,Field)
+ )
Field
Gdn 0 2 4 7
4 1 0 0 0
9 1 0 0 0
19 1 0 0 0
46 0 0 1 0
47 0 1 0 0
50 0 0 0 1
> fw = dat.frame(c(9,25,15,2,14,25,24,47,2,3,5,9,14,24,29,34), ncol = 2,
dimnames = list(c('taw','torridge','ouse','exe','lyn','brook','ditch'.,
Error: unexpected symbol in "fw =
dat.frame(c(9,25,15,2,14,25,24,47,2,3,5,9,14,24,29,34), ncol = 2,
dimnames = list(c('taw','torridge','ouse','exe','lyn','brook','ditch'."
> fw = data.frame(c(9,25,15,2,14,25,24,47,2,3,5,9,14,24,29,34), ncol = 2,
dimnames =
list(c('taw','torridge','ouse','exe','lyn','brook','ditch','fal'),c('coun
t','speed')))
> fw
c.9..25..15..2..14..25..24..47..2..3..5..9..14..24..29..34. ncol
dimnames.c..taw....torridge....ouse....exe....lyn....brook....ditch...
1 9 2
taw
2 25 2
torridge
3 15 2
ouse
4 2 2
exe
5 14 2
lyn
6 25 2
brook
7 24 2
ditch
8 47 2
fal
9 2 2
taw
10 3 2
torridge
11 5 2
ouse
12 9 2
exe
13 14 2
lyn
14 24 2
brook
15 29 2
ditch
16 34 2
fal
dimnames.c..count....speed..
1 count
2 speed
3 count
4 speed
5 count
6 speed
7 count
8 speed
9 count
10 speed
11 count
12 speed
13 count
14 speed
15 count
16 speed
> mf
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> height = c(5,6,7,9,11,14,15,17,19,28,31,32)
> hi = c(0,0,0,0,0,0,0,0,0,1,1,1)
> lo = c(1,2,1,1,1,0,0,0,0,0,0,0)
> mid = c(0,0,0,0,0,2,1,2,1,0,0,0)
> pw = data.frame(height,hi,lo,mid)
> pw
height hi lo mid
1 5 0 1 0
2 6 0 2 0
3 7 0 1 0
4 9 0 1 0
5 11 0 1 0
6 14 0 0 2
7 15 0 0 1
8 17 0 0 2
9 19 0 0 1
10 28 1 0 0
11 31 1 0 0
12 32 1 0 0
> table(pw)
, , lo = 0, mid = 0

hi
height 0 1
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

, , lo = 1, mid = 0

hi
height 0 1
5 1 0
6 0 0
7 1 0
9 1 0
11 1 0
14 0 0
15 0 0
17 0 0
19 0 0
28 0 0
31 0 0
32 0 0

, , lo = 2, mid = 0
hi
height 0 1
5 0 0
6 1 0
7 0 0
9 0 0
11 0 0
14 0 0
15 0 0
17 0 0
19 0 0
28 0 0
31 0 0
32 0 0

, , lo = 0, mid = 1

hi
height 0 1
5 0 0
6 0 0
7 0 0
9 0 0
11 0 0
14 0 0
15 1 0
17 0 0
19 1 0
28 0 0
31 0 0
32 0 0

, , lo = 1, mid = 1

hi
height 0 1
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 0
31 0 0
32 0 0

, , lo = 2, mid = 1

hi
height 0 1
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 0
31 0 0
32 0 0

, , lo = 0, mid = 2

hi
height 0 1
5 0 0
6 0 0
7 0 0
9 0 0
11 0 0
14 1 0
15 0 0
17 1 0
19 0 0
28 0 0
31 0 0
32 0 0

, , lo = 1, mid = 2

hi
height 0 1
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 0
31 0 0
32 0 0

, , lo = 2, mid = 2

hi
height 0 1
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 0
31 0 0
32 0 0

> str(pw)
'data.frame': 12 obs. of 4 variables:
$ height: num 5 6 7 9 11 14 15 17 19 28 ...
$ hi : num 0 0 0 0 0 0 0 0 0 1 ...
$ lo : num 1 2 1 1 1 0 0 0 0 0 ...
$ mid : num 0 0 0 0 0 2 1 2 1 0 ...
> mf
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> as.table(as.matrix(mf))
len sp alg no3 bod)\n
A 20.00 12.00 40.00 2.25 200.00
B 21.00 14.00 45.00 2.14 180.00
C 22.00 12.00 45.00 1.33 135.00
D 23.00 16.00 16.00 1.66 120.00
E 21.00 20.00 20.00 1.78 110.00
F 20.00 21.00 21.00 2.45 120.00
> as.table(mf)
Error in as.table.default(mf) : cannot coerce to a table
> is.table(bird)
[1] FALSE
> height = c(9,11,6,14,17,19,28,31,32,7,6,5,14,17,15,44,38,37)
> plant = c('vulgaris','sativa')
> water = c('lo','mid','hi')
> pw = data.frame(height,plant,water)
> pw
height plant water
1 9 vulgaris lo
2 11 sativa mid
3 6 vulgaris hi
4 14 sativa lo
5 17 vulgaris mid
6 19 sativa hi
7 28 vulgaris lo
8 31 sativa mid
9 32 vulgaris hi
10 7 sativa lo
11 6 vulgaris mid
12 5 sativa hi
13 14 vulgaris lo
14 17 sativa mid
15 15 vulgaris hi
16 44 sativa lo
17 38 vulgaris mid
18 37 sativa hi
> with(pw, ftable(height,plant,water))
water hi lo mid
height plant
5 sativa 1 0 0
vulgaris 0 0 0
6 sativa 0 0 0
vulgaris 1 0 1
7 sativa 0 1 0
vulgaris 0 0 0
9 sativa 0 0 0
vulgaris 0 1 0
11 sativa 0 0 1
vulgaris 0 0 0
14 sativa 0 1 0
vulgaris 0 1 0
15 sativa 0 0 0
vulgaris 1 0 0
17 sativa 0 0 1
vulgaris 0 0 1
19 sativa 1 0 0
vulgaris 0 0 0
28 sativa 0 0 0
vulgaris 0 1 0
31 sativa 0 0 1
vulgaris 0 0 0
32 sativa 0 0 0
vulgaris 1 0 0
37 sativa 1 0 0
vulgaris 0 0 0
38 sativa 0 0 0
vulgaris 0 0 1
44 sativa 0 1 0
vulgaris 0 0 0
> with(pw, ftable(height,water,plant))
plant sativa vulgaris
height water
5 hi 1 0
lo 0 0
mid 0 0
6 hi 0 1
lo 0 0
mid 0 1
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 0
mid 1 0
14 hi 0 0
lo 1 1
mid 0 0
15 hi 0 1
lo 0 0
mid 0 0
17 hi 0 0
lo 0 0
mid 1 1
19 hi 1 0
lo 0 0
mid 0 0
28 hi 0 0
lo 0 1
mid 0 0
31 hi 0 0
lo 0 0
mid 1 0
32 hi 0 1
lo 0 0
mid 0 0
37 hi 1 0
lo 0 0
mid 0 0
38 hi 0 0
lo 0 0
mid 0 1
44 hi 0 0
lo 1 0
mid 0 0
> ftable(plant ~ height + water, data = pw)
plant sativa vulgaris
height water
5 hi 1 0
lo 0 0
mid 0 0
6 hi 0 1
lo 0 0
mid 0 1
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 0
mid 1 0
14 hi 0 0
lo 1 1
mid 0 0
15 hi 0 1
lo 0 0
mid 0 0
17 hi 0 0
lo 0 0
mid 1 1
19 hi 1 0
lo 0 0
mid 0 0
28 hi 0 0
lo 0 1
mid 0 0
31 hi 0 0
lo 0 0
mid 1 0
32 hi 0 1
lo 0 0
mid 0 0
37 hi 1 0
lo 0 0
mid 0 0
38 hi 0 0
lo 0 0
mid 0 1
44 hi 0 0
lo 1 0
mid 0 0
> ftable(water ~ height + plant,data = pw)
water hi lo mid
height plant
5 sativa 1 0 0
vulgaris 0 0 0
6 sativa 0 0 0
vulgaris 1 0 1
7 sativa 0 1 0
vulgaris 0 0 0
9 sativa 0 0 0
vulgaris 0 1 0
11 sativa 0 0 1
vulgaris 0 0 0
14 sativa 0 1 0
vulgaris 0 1 0
15 sativa 0 0 0
vulgaris 1 0 0
17 sativa 0 0 1
vulgaris 0 0 1
19 sativa 1 0 0
vulgaris 0 0 0
28 sativa 0 0 0
vulgaris 0 1 0
31 sativa 0 0 1
vulgaris 0 0 0
32 sativa 0 0 0
vulgaris 1 0 0
37 sativa 1 0 0
vulgaris 0 0 0
38 sativa 0 0 0
vulgaris 0 0 1
44 sativa 0 1 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 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0
vulgaris 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0
lo sativa 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1
vulgaris 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0
mid sativa 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0
vulgaris 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0
> with(pw, ftable(water, plant, height))
height 5 6 7 9 11 14 15 17 19 28 31 32 37 38 44
water plant
hi sativa 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0
vulgaris 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0
lo sativa 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1
vulgaris 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0
mid sativa 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0
vulgaris 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0
> gr.t = ftable(plant ~ height + water, data = pw)
> gr.t
plant sativa vulgaris
height water
5 hi 1 0
lo 0 0
mid 0 0
6 hi 0 1
lo 0 0
mid 0 1
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 0
mid 1 0
14 hi 0 0
lo 1 1
mid 0 0
15 hi 0 1
lo 0 0
mid 0 0
17 hi 0 0
lo 0 0
mid 1 1
19 hi 1 0
lo 0 0
mid 0 0
28 hi 0 0
lo 0 1
mid 0 0
31 hi 0 0
lo 0 0
mid 1 0
32 hi 0 1
lo 0 0
mid 0 0
37 hi 1 0
lo 0 0
mid 0 0
38 hi 0 0
lo 0 0
mid 0 1
44 hi 0 0
lo 1 0
mid 0 0
> gr.t[1:3,]
[,1] [,2]
[1,] 1 0
[2,] 0 0
[3,] 0 0
> gr.sub = gr.t[1:3,]
> gr.sub
[,1] [,2]
[1,] 1 0
[2,] 0 0
[3,] 0 0
> colnames(gr.sub) = c('sativa','vulgaris')
> rownames(gr.sub) = c('hi','lo','mid')
> gr.sub
sativa vulgaris
hi 1 0
lo 0 0
mid 0 0
> fr.sub = matrix(gr.t[1:3,],ncol = 2, dimnames =
list(c('hi','lo','mid'),c('sativa','vulgaris')
+ ))
> gr.sub
sativa vulgaris
hi 1 0
lo 0 0
mid 0 0
> fr.sub
sativa vulgaris
hi 1 0
lo 0 0
mid 0 0
> with(pw, ftable(height == 14, water, plant))
plant sativa vulgaris
water
FALSE hi 3 3
lo 2 2
mid 3 3
TRUE hi 0 0
lo 1 1
mid 0 0
> with(pw, ftable(height == 14, water == 'hi', plant))
plant sativa vulgaris

FALSE FALSE 5 5
TRUE 3 3
TRUE FALSE 1 1
TRUE 0 0
> pw.t = pw[which(pw$height == 14),]
> pw.t
height plant water
4 14 sativa lo
13 14 vulgaris lo
> with(pw.t, ftable(height, plant, water))
water lo
height plant
14 sativa 1
vulgaris 1
> if(class(gr.t) == 'ftable') TRUE else FALSE
[1] TRUE
> bird
Gdn Hegde Park Field Wood
Bbird 47 10 40 2 2
C.Finch 19 3 5 0 2
Gt.Tit 50 0 10 7 0
Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Thrush 4 0 6 0 0
> rowSums(bird)
Bbird C.Finch Gt.Tit Sparrow Robin Thrush
101 29 67 74 14 10
> apply( bird, MARGIN = 2, FUN = sum)
Gdn Hegde Park Field Wood
175 32 69 13 6
> margin.table(bird)
[1] 295
> margin.table(bird,1)
Bbird C.Finch Gt.Tit Sparrow Robin Thrush
101 29 67 74 14 10
> margin.table(bird,margin = 2)
Gdn Hegde Park Field Wood
175 32 69 13 6
> prop.table(bird)
Gdn Hegde Park Field Wood
Bbird 0.15932203 0.03389831 0.13559322 0.006779661 0.006779661
C.Finch 0.06440678 0.01016949 0.01694915 0.000000000 0.006779661
Gt.Tit 0.16949153 0.00000000 0.03389831 0.023728814 0.000000000
Sparrow 0.15593220 0.05423729 0.02711864 0.013559322 0.000000000
Robin 0.03050847 0.01016949 0.00000000 0.000000000 0.006779661
Thrush 0.01355932 0.00000000 0.02033898 0.000000000 0.000000000
> prop.table(bird,margin = 1)
Gdn Hegde Park Field Wood
Bbird 0.4653465 0.0990099 0.3960396 0.01980198 0.01980198
C.Finch 0.6551724 0.1034483 0.1724138 0.00000000 0.06896552
Gt.Tit 0.7462687 0.0000000 0.1492537 0.10447761 0.00000000
Sparrow 0.6216216 0.2162162 0.1081081 0.05405405 0.00000000
Robin 0.6428571 0.2142857 0.0000000 0.00000000 0.14285714
Thrush 0.4000000 0.0000000 0.6000000 0.00000000 0.00000000
> addmargins( bird, 1, mean)
Gdn Hegde Park Field Wood
Bbird 47.00000 10.000000 40.0 2.000000 2
C.Finch 19.00000 3.000000 5.0 0.000000 2
Gt.Tit 50.00000 0.000000 10.0 7.000000 0
Sparrow 46.00000 16.000000 8.0 4.000000 0
Robin 9.00000 3.000000 0.0 0.000000 2
Thrush 4.00000 0.000000 6.0 0.000000 0
mean 29.16667 5.333333 11.5 2.166667 1
> addmargins(bird, 2, median)
Gdn Hegde Park Field Wood median
Bbird 47 10 40 2 2 10
C.Finch 19 3 5 0 2 3
Gt.Tit 50 0 10 7 0 7
Sparrow 46 16 8 4 0 8
Robin 9 3 0 0 2 2
Thrush 4 0 6 0 0 0
> birds
Error: object 'birds' not found
> species
[1] "Bbird" "C.Finch" "Gt.Tit" "Sparrow" "Robin" "Thrush"
> habitat
Error: object 'habitat' not found
> habitat = c('garden','parkland','hedgerow','woodland','pasture')
> qty = c(47,19,50,46,9,4,40,5,10,8,6,10,3,16,3,2,2,2,2,7,4)
> birds = data.frame(species,habitat,qty)
Error in data.frame(species, habitat, qty) :
arguments imply differing number of rows: 6, 5, 21
> birds = data.frame(species,habitat,qty)
Error in data.frame(species, habitat, qty) :
arguments imply differing number of rows: 6, 5, 21
> birds = data.frame(qty,species,habitat)
Error in data.frame(qty, species, habitat) :
arguments imply differing number of rows: 21, 6, 5
> pw
height plant water
1 9 vulgaris lo
2 11 sativa mid
3 6 vulgaris hi
4 14 sativa lo
5 17 vulgaris mid
6 19 sativa hi
7 28 vulgaris lo
8 31 sativa mid
9 32 vulgaris hi
10 7 sativa lo
11 6 vulgaris mid
12 5 sativa hi
13 14 vulgaris lo
14 17 sativa mid
15 15 vulgaris hi
16 44 sativa lo
17 38 vulgaris mid
18 37 sativa hi
> with(pw, table(plant,water))
water
plant hi lo mid
sativa 3 3 3
vulgaris 3 3 3
> with(pw, table(plant,water,height))
, , height = 5

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

, , height = 6

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

, , height = 7

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

, , height = 9

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

, , height = 11

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

, , height = 14

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

, , height = 15

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

, , height = 17

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

, , height = 19

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

, , height = 28

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

, , height = 31

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

, , height = 32

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

, , height = 37

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

, , height = 38

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

, , height = 44

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

> with(pw, ftable(plant,water,height))


height 5 6 7 9 11 14 15 17 19 28 31 32 37 38 44
plant water
sativa hi 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0
lo 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1
mid 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0
vulgaris hi 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0
lo 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0
mid 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0
> pw.tt = xtabs(height ~ plant + water, data = pw)
> pw.tt
water
plant hi lo mid
sativa 61 65 59
vulgaris 53 51 61
> class(pw.tt)
[1] "xtabs" "table"
> if(any(class(pw.tt) == "xtabs") TRUE else FALSE
Error: unexpected numeric constant in "if(any(class(pw.tt) == "xtabs")
TRUE"
> if(any(class(pw.tt) == "xtabs")) TRUE else FALSE
[1] TRUE
> as.data.frame(pw.tt)
plant water Freq
1 sativa hi 61
2 vulgaris hi 53
3 sativa lo 65
4 vulgaris lo 51
5 sativa mid 59
6 vulgaris mid 61
> bird.df = as.data.frame(bird)
> bird.df
Gdn Hegde Park Field Wood
Bbird 47 10 40 2 2
C.Finch 19 3 5 0 2
Gt.Tit 50 0 10 7 0
Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Thrush 4 0 6 0 0
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> # CH=5 Data: Distribution
> dt1
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> table(dt1)
dt1
2 3 4 5 6 7 8 9
1 3 2 4 2 2 1 1
> stem(dt1)

The decimal point is at the |

2 | 0000
4 | 000000
6 | 0000
8 | 00

> stem(dt1, scale = 2)

The decimal point is at the |

2 | 0
3 | 000
4 | 00
5 | 0000
6 | 00
7 | 00
8 | 0
9 | 0

> data4
[1] 23.0 17.0 12.5 11.0 18.0 12.0 14.5 9.0 11.0 9.0 12.5 14.5 17.0
8.0 21.0
> stem(data4)

The decimal point is 1 digit(s) to the right of the |

0 | 899
1 | 11233
1 | 55778
2 | 13

> stem(data4, scale = 2)


The decimal point is at the |

8 | 000
10 | 00
12 | 055
14 | 55
16 | 00
18 | 0
20 | 0
22 | 0

> hist(dt1)
> hist(dt1, col = 'gray75',main=NULL, xlab = 'Size class for dt1',ylim =
c(0,0.3),freq = FALSE)
>
> hist(dt1, col = 'gray75',main=NULL, xlab = 'Size class for dt1',ylim =
c(0,0.3),freq = FALSE)
> dens = density(dt1)
> dens

Call:
density.default(x = dt1)

Data: dt1 (16 obs.); Bandwidth 'bw' = 0.9644

x y
Min. :-0.8932 Min. :0.0002982
1st Qu.: 2.3034 1st Qu.:0.0134042
Median : 5.5000 Median :0.0694574
Mean : 5.5000 Mean :0.0781187
3rd Qu.: 8.6966 3rd Qu.:0.1396352
Max. :11.8932 Max. :0.1798531
> names(dens)
[1] "x" "y" "bw" "n" "call"
"data.name" "has.na"
> str(dt1)
num [1:16] 3 5 7 5 3 2 6 8 5 6 ...
> str(dens)
List of 7
$ x : num [1:512] -0.893 -0.868 -0.843 -0.818 -0.793 ...
$ y : num [1:512] 0.000313 0.000339 0.000367 0.000397 0.000429
...
$ bw : num 0.964
$ n : int 16
$ call : language density.default(x = dt1)
$ data.name: chr "dt1"
$ has.na : logi FALSE
- attr(*, "class")= chr "density"
> plot(dens$x, dens$y)
> plot(density(dt1))
> fw
c.9..25..15..2..14..25..24..47..2..3..5..9..14..24..29..34. ncol
dimnames.c..taw....torridge....ouse....exe....lyn....brook....ditch...
1 9 2
taw
2 25 2
torridge
3 15 2
ouse
4 2 2
exe
5 14 2
lyn
6 25 2
brook
7 24 2
ditch
8 47 2
fal
9 2 2
taw
10 3 2
torridge
11 5 2
ouse
12 9 2
exe
13 14 2
lyn
14 24 2
brook
15 29 2
ditch
16 34 2
fal
dimnames.c..count....speed..
1 count
2 speed
3 count
4 speed
5 count
6 speed
7 count
8 speed
9 count
10 speed
11 count
12 speed
13 count
14 speed
15 count
16 speed
> mf
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> boxplot(mf$alg)
> hist(mf)
Error in hist.default(mf) : 'x' must be numeric
> hist(dt1)
> hist(dt1)
> hist(dt1)
>
R version 4.3.2 (2023-10-31 ucrt) -- "Eye Holes"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.


Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> plot(density(dt1))
> hist(dt1,freq = F,col = 'gray85')
> lines(density(dt1), lty =2)
> lines(density(dt1, k= 'rectangular'))
> rnorm(20, mean = 5, sd =1)
[1] 4.961028 4.103988 4.395820 5.153047 4.963701 6.049925 4.218205
5.693877
[9] 4.051571 3.951315 4.544928 6.077120 4.609351 4.902176 4.143953
6.081976
[17] 6.567138 6.117462 4.304101 5.347486
> pnorm(5, mean =5, sd = 1)
[1] 0.5
> qnorm(0.5,5,1)
[1] 5
> dnorm(c(4,5,6),mean = 5, sd = 1)
[1] 0.2419707 0.3989423 0.2419707
> qnorm(c(0.05,0.95), mean = 5,sd =1 )
[1] 3.355146 6.644854
> hist(dt1.norm, freq = F, border = 'gray50',main = 'comparing two
distribution',xlab = 'data2 size classes')
Error: object 'dt1.norm' not found
> dt1.norm = rnorm(1000, mean(dt1), sd(dt1))
> hist(dt1.norm, freq = F, border = 'gray50',main = 'comparing two
distribution',xlab = 'data2 size classes')
> lines(density(dt1), lwd =2)
> rpois = (50, lambda = 10)
Error: unexpected ',' in "rpois = (50,"
> rpois(50 ,lambda = 10)
[1] 11 3 12 9 7 11 13 12 9 8 8 12 9 13 13 12 11 8 16 11 12 11 10
10 9
[26] 9 15 9 13 10 2 8 14 13 10 13 8 7 6 6 8 13 14 14 9 7 6 12
10 9
> pbinom(c(3,6,9,12), size = 17 ,prob =0.5)
[1] 0.006362915 0.166152954 0.685470581 0.975479126
> qt(0.975, df= c(5,10,100,Inf))
[1] 2.570582 2.228139 1.983972 1.959964
> (1-pt(c(1.6,1.9,2.2),df = Inf))*2
[1] 0.10959858 0.05743312 0.02780690
> pt(c(1.6,1.9,2.2),Inf)
[1] 0.9452007 0.9712834 0.9860966
> pt(c(1.6,1.9,2.2),Inf, lower.tail = FALSE)
[1] 0.05479929 0.02871656 0.01390345
> pt(c(1.6,1.9,2.2),Inf,lower.tail = FALSE)*2
[1] 0.10959858 0.05743312 0.02780690
> pf(seq(3,5,0.5),df1 = 2,df2 = 12, lower.tail= F)
[1] 0.08779150 0.06346962 0.04665600 0.03481543 0.02633610
> runif(10)
[1] 0.62719180 0.59776828 0.35578051 0.65856292 0.68811773 0.27971730
[7] 0.51853838 0.92647277 0.05495673 0.66096811
> runif(10,min=0, max=10)
[1] 5.8152710 3.2479174 9.4814383 6.6966261 0.7600207 5.3118883
9.8860684
[8] 8.6648400 6.2775698 2.2504481
> punif(6,min=0,max=10)
[1] 0.6
> RNGkind()
[1] "Mersenne-Twister" "Inversion" "Rejection"
> RNGkind(kind = 'Super',normal.kind = 'Box')
> RNGkind()
[1] "Super-Duper" "Box-Muller" "Rejection"
> Rngkind('default')
Error in Rngkind("default") : could not find function "Rngkind"
> RNGkind()
[1] "Super-Duper" "Box-Muller" "Rejection"
> RNGkind('default')
> RNGkind()
[1] "Mersenne-Twister" "Box-Muller" "Rejection"
> RNGkind('default','default')
> RNGkind()
[1] "Mersenne-Twister" "Inversion" "Rejection"
> set.seed()
Error in set.seed() : argument "seed" is missing, with no default
> set.seed(1)
> runif(1)
[1] 0.2655087
> runif(1)
[1] 0.3721239
> runif(1)
[1] 0.5728534
> set.seed(1)
> runif(3)
[1] 0.2655087 0.3721239 0.5728534
> set.seed(1, kind = 'Super')
> runif(3)
[1] 0.3714075 0.4789723 0.9636913
> RNGkind()
[1] "Super-Duper" "Inversion" "Rejection"
> set.seed(1,kind= 'default')
> runif(3)
[1] 0.2655087 0.3721239 0.5728534
> RNGkind()
[1] "Mersenne-Twister" "Inversion" "Rejection"
> dt1
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> sample(dt1,size=4)
[1] 3 5 5 6
> sample(data8, size = 4,replace = TRUE)
Error: object 'data8' not found
> data8
Error: object 'data8' not found
> sample(dt1[dt1 > 5], size = 3)
[1] 8 6 7
> sample(dt1[dt1>5])
[1] 8 7 9 6 6 7
> dt1[dt1>5]
[1] 7 6 8 6 9 7
> sample(dt1[dt1>5])
[1] 7 6 8 7 6 9
> sample(dt1[dt1>5])
[1] 9 7 7 6 6 8
> sample(dt1[dt1>5])
[1] 6 7 9 7 6 8
> sample(dt1[dt1 > 8])
[1] 1 4 3 6 2 5 8 9 7
> set.seed(4)
> sample(dt1,size = 3)
[1] 8 9 7
> set.seed(4)
> resample(dt1,size = 3)
Error in resample(dt1, size = 3) : could not find function "resample"
> sample(dt1[dt1>8])
[1] 8 3 9 7 4 6 2 1 5
> shapiro.test(dt1)

Shapiro-Wilk normality test

data: dt1
W = 0.96332, p-value = 0.7223

> shapiro.test(rpois(100, lambda =5))

Shapiro-Wilk normality test

data: rpois(100, lambda = 5)


W = 0.9735, p-value = 0.04122

> mf
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> shapiro.test(mf$alg)

Shapiro-Wilk normality test

data: mf$alg
W = 0.81355, p-value = 0.07755

> ks.test(dt1,'pnorm',mean=5,sd=2)

Asymptotic one-sample Kolmogorov-Smirnov test

data: dt1
D = 0.125, p-value = 0.9639
alternative hypothesis: two-sided

Warning message:
In ks.test.default(dt1, "pnorm", mean = 5, sd = 2) :
ties should not be present for the Kolmogorov-Smirnov test
> ks.test(dt1,pnorm(20,5,2))

Exact two-sample Kolmogorov-Smirnov test

data: dt1 and pnorm(20, 5, 2)


D = 1, p-value = 0.1176
alternative hypothesis: two-sided

> ks.test(dt1,'ppois',5)

Asymptotic one-sample Kolmogorov-Smirnov test

data: dt1
D = 0.24096, p-value = 0.3108
alternative hypothesis: two-sided

Warning message:
In ks.test.default(dt1, "ppois", 5) :
ties should not be present for the Kolmogorov-Smirnov test
> qqnorm(dt1)
> qqnorm(dt1,main='QQ plot of example data',xlab='Theoritical'
+ ,ylab='Quantiles for dt1')
> qqline(dt1,lwd = 2,lty=2)
> qqplot(rpois(50,5),rnorm(50,5,1))
> qqplot(dt1,dt1)
> qqp = qqplot(dt1,rnorm(50,5,2))
> qqp
$x
[1] 2 3 3 3 4 4 5 5 5 5 6 6 7 7 8 9

$y
[1] 0.4841236 1.7161045 2.6810093 3.2554153 3.4037517 3.6608763
4.0070002
[8] 4.1174306 4.8623181 5.7228188 6.1032897 6.3309340 6.8435012
7.0733509
[15] 7.6626950 8.5921276

> abline(lm(qqp$y ~ qqp$x))


>
R version 4.3.2 (2023-10-31 ucrt) -- "Eye Holes"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.


Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> # CH=7 Introduction to Graphical Analysis


> mf
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> boxplot(mf$alg)
> boxplot(mf$alg,mf$sp)
> boxplot(mf$alg, mf$sp, names = c('count', 'speed'))
> title(xlab = 'Variable', ylab = 'Value')
> boxplot(mf$alg, mf$sp, names = c('count', 'speed'), range = 0, xlab =
'Variable', ylab = 'Value', col = 'gray90')
> rich = c(12,15,17,11,15,8,9,7,9)
> graze = C('mow','unmow')
Error in `contrasts<-`(`*tmp*`, value = contr) :
contrasts can be applied only to factors with 2 or more levels
> graze = c('mow','unmow')
> grass = data.frame(rich,graze)
Error in data.frame(rich, graze) :
arguments imply differing number of rows: 9, 2
> graze =
c('mow','mow','mow','mow','mow','unmow','unmow','unmow','unmow')
> grass = data.frame(rich,graze)
> 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
> boxplot(rich ~ graze, data = grass, range = 0)
> title(xlab = 'cutting treatment', ylab = 'species richness')
> boxplot(rich ~ graze, data = grass, range = 0, horizontal = TRUE)
> title(ylab = 'cutting treatment', xlab = 'species richness')
> fw
c.9..25..15..2..14..25..24..47..2..3..5..9..14..24..29..34. ncol
1 9 2
2 25 2
3 15 2
4 2 2
5 14 2
6 25 2
7 24 2
8 47 2
9 2 2
10 3 2
11 5 2
12 9 2
13 14 2
14 24 2
15 29 2
16 34 2
dimnames.c..taw....torridge....ouse....exe....lyn....brook....ditch...
1 taw
2 torridge
3 ouse
4 exe
5 lyn
6 brook
7 ditch
8 fal
9 taw
10 torridge
11 ouse
12 exe
13 lyn
14 brook
15 ditch
16 fal
dimnames.c..count....speed..
1 count
2 speed
3 count
4 speed
5 count
6 speed
7 count
8 speed
9 count
10 speed
11 count
12 speed
13 count
14 speed
15 count
16 speed
> count =c(9,25,15,2,14,25,24,47)
> speed = c(2,3,,5,9,14,24,29,34)
Error in c(2, 3, , 5, 9, 14, 24, 29, 34) : argument 3 is empty
> speed = c(2,3,5,9,14,24,29,34)
> fw = data.frame(count,speed)
> fw
count speed
1 9 2
2 25 3
3 15 5
4 2 9
5 14 14
6 25 24
7 24 29
8 47 34
> plot(fw$speed, fw$count, xlab = "", ylab = "")
> plot(fw$speed, fw$count, xlab = 'Speed m/s', ylab = 'Count of Mayfly')
> plot(fw$speed, fw$count, xlab = 'Speed m/s', ylab = 'Count of Mayfly',
pch = 18, cex = 2, col = 'gray50', xlim = c(0, 50), ylim = c(0, 50))
> plot(count ~ speed, data = fw)
> plot(count ~ speed, data = fw, xlab = 'Speed m/s', ylab = 'Count of
Mayfly', pch = 18, cex = 2, col = 'gray50', xlim = c(0, 50), ylim = c(0,
50))
> abline(lm(count ~ speed, data = fw), lty = 'dotted', lwd = 2, col =
'gray50')
>
R version 4.3.2 (2023-10-31 ucrt) -- "Eye Holes"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.


Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> fw
count speed
1 9 2
2 25 3
3 15 5
4 2 9
5 14 14
6 25 24
7 24 29
8 47 34
> plot(fw)
> head(mf)
len sp alg no3 bod)\n
1 20 12 40 2.25 200
2 21 14 45 2.14 180
3 22 12 45 1.33 135
4 23 16 16 1.66 120
5 21 20 20 1.78 110
6 20 21 21 2.45 120
> plot(mf)
> pairs(~ Length + Speed + NO3, data = mf)
Error in eval(predvars, data, env) : object 'Length' not found
> pairs(~ len + sp + no3, data = mf)
> pairs(~ len + sp + no3, data = mf , col ='red', cex = 2, pch = 'X')
> data11
[1] 3 5 7 5 3 2 6 8 5 6 9 8
> data8 = data5
> data8
[1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov"
"dec"
> pie(data11, labels = data8)
> pc = c('gray40', 'gray50', 'gray60', 'gray70', 'gray80', 'gray90'
+ )
> pie(data11, labels = data8, col = pc, clockwise = TRUE, init.angle =
180)
> fw
count speed
1 9 2
2 25 3
3 15 5
4 2 9
5 14 14
6 25 24
7 24 29
8 47 34
> pc = c('gray65', 'gray70', 'gray75', 'gray80', 'gray85', 'gray90')
> pie(fw$count, labels = row.names(fw), col = pc, cex = 1.2)
> bird
Gdn Hegde Park Field Wood
Bbird 47 10 40 2 2
C.Finch 19 3 5 0 2
Gt.Tit 50 0 10 7 0
Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Thrush 4 0 6 0 0
> pie(bird[,1], col = pc)
> dotchart(data11, labels = data8)
> bird
Gdn Hegde Park Field Wood
Bbird 47 10 40 2 2
C.Finch 19 3 5 0 2
Gt.Tit 50 0 10 7 0
Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Thrush 4 0 6 0 0
> dotchart(bird)
> dotchart(t(bird))
> dotchart(bird, color = 'gray30', gcolor = 'black', lcolor = 'gray30',
cex = 0.8, xlab = 'Bird Counts', bg = 'gray90', pch = 21)
> dotchart(bird, gdata = colMeans(bird), gpch = 16, gcolor = 'blue')
> mtext('Grouping = mean', side =3, adj = 1)
> title(main = 'Bird species and Habitat')
> title(xlab = 'Bird abundance')
> rain = data11
> rain
[1] 3 5 7 5 3 2 6 8 5 6 9 8
> barplot(rain)
> barplot(rain, names = data8)
> barplot(rain, xlab = 'Month', ylab = 'Rainfall cm', ylim = c(0,10))
> barplot(rain, xlab = 'Month', ylab = 'Rainfall cm', ylim = c(0,10), col
= 'lightblue')
> abline(h = seq(1,9,2), lty = 2, lwd = 0.5, col = 'gray40')
> box()
> table(rain)
rain
2 3 5 6 7 8 9
1 2 3 2 1 2 1
> barplot(table(rain), ylab = 'Frequency', xlab = 'Numeric category')
> abline(h=0)
> barplot(fw$count, names = row.names(fw), ylab = 'Invertebrate Count' ,
col = 'tan')
> abline(h=0)
> barplot(bird)
> barplot(bird, beside = TRUE, ylab = 'Total birds counted', xlab =
'Habitat')
> barplot(bird, beside = TRUE, legend = TRUE)
> title(ylab = 'Total birds counted', xlab = 'Habitat')
> barplot(bird, beside = TRUE, legend = TRUE, col = c('black', 'pink',
'lightblue', 'tan', 'red', 'brown'))
> barplot(t(bird), beside = TRUE, legend = TRUE, cex.names = 0.8, col =
c('black', 'pink', 'lightblue', 'tan', 'red', 'brown'))
> title(ylab = 'Bird Count', xlab = 'Bird Species')
> barplot(bird, beside = TRUE, horiz = TRUE)
> bccol = c('black', 'pink', 'lightblue', 'tan', 'red', 'brown')
> barplot(bird, beside = TRUE, legend = TRUE, horiz = TRUE, xlim = c(0,
60), col = bccol)
> title(ylab = 'Habitat', xlab = 'Bird count')
> barplot(apply(mf, 2, median, na.rm = T), ylab = 'Median Measurement')
>
Histogram of dt1
4
3
Frequency

2
1
0

2 3 4 5 6 7 8 9

dt1

●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●

●●
●●
● ●
●●
●●

●●
●● ●●
●●

●●
● ●●
●●

●●
● ●●

●●
● ●●


●●
● ●
●●

●●
● ●●


●●
● ●
●●
●●
● ●●

●● ●●


0.15


●● ●

●●
● ●
●●

●● ●●
●●
● ●●
●● ●●
●● ●●
●● ●●
●● ●●
●● ●●

●● ●

●● ●
●●
●● ●●
●● ●●
●● ●●

●● ●
●●
●● ●●

●● ●
●●
●● ●●

●● ●
●●
●● ●●
●●
0.10

●● ●●
● ●●

● ●
dens$y

● ●●


● ●

● ●
●●
●● ●●

● ●
●●

● ●●

● ●
●●
●● ●●

● ●
●●

● ●●

● ●
●●
●● ●●

● ●
●●

● ●●

● ●
●●
●● ●●

● ●
●●

● ●●

● ●
●●
0.05

●● ●●

●● ●
●●
● ●●


● ●
●●

● ●●


● ●
●●
● ●●

●● ●
●●
●● ●●

●● ●
●●
●● ●●


●● ●
●●

● ●●


● ●
●●

●●
● ●
●●

●● ●●
●●
●●
● ●●
●●
●●
●● ●●
●●
●●
●● ●●
●●

●●
●● ●
●●
●●
●●
●●
●● ●●
●●
●●
●●
●●
●● ●●
●●
●●
●●
● ●
0.00

●●
●●
●●
●●
●●
● ●
●●
●●
●●
●●
●●

●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●● ●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●

0 2 4 6 8 10 12

dens$x
density(x = dt1)
0.15
0.10
Density

0.05
0.00

0 2 4 6 8 10 12

N = 16 Bandwidth = 0.9644
Density

0.00 0.05 0.10 0.15 0.20 0.25

2
3
4
5

dt1
Histogram of dt1

6
7
8
9
comparing two distribution
0.20
0.15
Density

0.10
0.05
0.00

0 2 4 6 8 10 12

data2 size classes


Normal Q−Q Plot


9


8

● ●
7
Sample Quantiles

● ●
6

● ● ● ●
5

● ●
4

● ● ●
3


2

−2 −1 0 1 2

Theoretical Quantiles
QQ plot of example data


9


8

● ●
7
Quantiles for dt1

● ●
6

● ● ● ●
5

● ●
4

● ● ●
3


2

−2 −1 0 1 2

Theoritical

9


8


7


6
dt1


5


4


3


2

2 3 4 5 6 7 8 9

dt1

8




6


rnorm(50, 5, 2)



4




2

2 3 4 5 6 7 8 9

dt1
16
14
richness
speciesrich

12
10
8

mow unmow

cuttinggraze
treatment
cuttinggraze
treatment

mow unmow

8
10
12

speciesrich
richness
14
16

40
30
Count of Mayfly

● ●

20



10

5 10 15 20 25 30 35

Speed m/s
Count of Mayfly

0 10 20 30 40 50

0
10
20

Speed m/s
30
40
50
Count of Mayfly

0 10 20 30 40 50

0
10
20

Speed m/s
30
40
50

40
30
count

● ●

20



10

5 10 15 20 25 30 35

speed
15 20 25 30 35 40 45
15 20 25 30 35 40 45

1
2
Value

15 20 25 30 35 40 45

count

Variable
speed
35


30


25


20
speed

15


10


5


10 20 30 40

count
12 16 20 1.4 1.8 2.2

23.0
● ● ● ●

● ● ● ●

21.5
len ● ● ● ● ● ● ● ●

20.0
● ● ● ● ● ● ● ●

● ● ● ●
20

● ● ● ●

sp
16

● ● ● ●

● ● ● ●
12

● ● ● ● ● ● ● ●

45
● ● ● ● ● ● ● ●
● ● ● ●

35
alg

25
● ● ● ● ● ● ● ●

15
● ● ● ●

● ● ● ●
2.2

● ● ● ●
● ● ● ●

no3
1.8

● ● ● ●
● ● ● ●
1.4

● ● ● ●

200
● ● ● ●

● ● ● ●
bod)

160
● ● ● ●

120
● ● ● ● ● ● ● ●
● ● ● ●

20.0 21.5 23.0 15 25 35 45 120 160 200


12 14 16 18 20

23.0
● ●

22.0
● ●

len

21.0
● ● ● ●

20.0
● ● ● ●

● ●
20

● ●
18

sp
16

● ●
14

● ●
12

● ● ● ●

● ●

● ●

2.2
● ●

no3

1.8
● ●

● ●

1.4
● ●

20.0 21.0 22.0 23.0 1.4 1.8 2.2


12 14 16 18 20

23.0
X X

22.0
X X
len

21.0
X X X X

20.0
X X X X
X X
X X
20
18

sp
X X
16

X X
14

X X X X
12

X X
X X

2.2
X X
no3

1.8
X X
X X

1.4
X X
20.0 21.0 22.0 23.0 1.4 1.8 2.2
apr
may mar
jun

jul feb

jan

aug

dec

sep

oct nov
apr
mar may
jun

feb jul

jan

aug

dec

sep

nov oct
3
4
5 2

6 1

7 8
C.Finch
Bbird

Thrush
Gt.Tit
Robin

Sparrow
dec ●

nov ●

oct ●

sep ●

aug ●

jul ●

jun ●

may ●

apr ●

mar ●

feb ●

jan ●

2 3 4 5 6 7 8 9
Gdn
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

Hegde
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

Park
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

Field
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

Wood
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

0 10 20 30 40 50
Bbird
Wood ●
Field ●
Park ●
Hegde ●
Gdn ●

C.Finch
Wood ●
Field ●
Park ●
Hegde ●
Gdn ●

Gt.Tit
Wood ●
Field ●
Park ●
Hegde ●
Gdn ●

Sparrow
Wood ●
Field ●
Park ●
Hegde ●
Gdn ●

Robin
Wood ●
Field ●
Park ●
Hegde ●
Gdn ●

Thrush
Wood ●
Field ●
Park ●
Hegde ●
Gdn ●

0 10 20 30 40 50
Bird species and Habitat
Gdn ● Grouping = mean
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

Hegde ●
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

Park ●
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

Field ●
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

Wood ●
Thrush ●
Robin ●
Sparrow ●
Gt.Tit ●
C.Finch ●
Bbird ●

0 10 20 30 40 50
Bird abundance
8
6
4
2
0

jan feb mar apr may jun jul aug sep oct nov dec
Rainfall cm

0 2 4 6 8 10

Month
Invertebrate Count

0 10 20 30 40

1
2
3
4
5
6
7
8
150
100
50
0

Gdn Hegde Park Field Wood


50

Bbird
C.Finch
Gt.Tit
40

Sparrow
Robin
Thrush
30
20
10
0

Gdn Hegde Park Field Wood


50

Gdn
Hegde
Park
40

Field
Wood
30
Bird Count

20
10
0

Bbird C.Finch Gt.Tit Sparrow Robin Thrush

Bird Species
Median Measurement

0 20 40 60 80 100 120

len
sp
alg
no3
bod)

You might also like