This session provides an introduction to the most basic operations and functions in R.
You can access the full slideshow used in the 1-Introduction narration here.
The dataset called ‘coralreefherbivores.csv’ can be downloaded here.
[,1] | [,2] | [,3] | [,4] | [,5] |
---|---|---|---|---|
[1,] | 1 | 2 | 3 | 4 |
[2,] | 5 | 6 | 7 | 8 |
[3,] | 9 | 10 | 11 | 12 |
[4,] | 13 | 14 | 15 | 16 |
[5,] | 17 | 18 | 19 | 20 |
create a sequence of multiples of nines between 0 and 100, repeated ten times
create a vector with random values using rnorm(1000, 0) , with an explanation of what rnorm() does
create a second vector using rnorm, but this time with 10,000 elements, a mean of 50, and a standard deviation of 10
tell me what the following functions tell you about your vectors: length(), str(), summary(), head(), tail()
retain only the elements from your previous vector that are greater than the mean but smaller than the 3rd Quantile
[] | redfish | bluefish | whitefish | yellowfish |
---|---|---|---|---|
S1 | 0 | 3 | 6 | 9 |
S2 | 12 | 15 | 18 | 21 |
S3 | 24 | 27 | 30 | 33 |
S4 | 24 | 22 | 20 | 18 |
S5 | 16 | 14 | 12 | 10 |
S6 | 8 | 6 | 4 | 2 |
turn your matrix into a data.frame and add a column entitled “Location” that contains the following sequence: Texas, Belize, Texas, Belize, Texas
determine the mean number of fish at site S3
determine the difference in the mean number of bluefish and yellowfish across all sites
-a)
[1] 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6
[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
[22] 23 24 25 26 27 28 29 30 31 32 33 34 35 36 2 3 4 5 6 7 8
[43] 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
[64] 30 31 32 33 34 35 36 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[85] 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[1] "bluefish" "redfish" "whitefish" "bluefish" "redfish"
[6] "whitefish" "bluefish" "redfish" "whitefish" "bluefish"
[11] "redfish" "whitefish" "bluefish" "redfish" "whitefish"
[16] "bluefish" "redfish" "whitefish"
a4 <- paste(a2, a3)
a4
[1] "2 bluefish" "3 redfish" "4 whitefish" "5 bluefish"
[5] "6 redfish" "7 whitefish" "8 bluefish" "9 redfish"
[9] "10 whitefish" "11 bluefish" "12 redfish" "13 whitefish"
[13] "14 bluefish" "15 redfish" "16 whitefish" "17 bluefish"
[17] "18 redfish" "19 whitefish" "20 bluefish" "21 redfish"
[21] "22 whitefish" "23 bluefish" "24 redfish" "25 whitefish"
[25] "26 bluefish" "27 redfish" "28 whitefish" "29 bluefish"
[29] "30 redfish" "31 whitefish" "32 bluefish" "33 redfish"
[33] "34 whitefish" "35 bluefish" "36 redfish" "2 whitefish"
[37] "3 bluefish" "4 redfish" "5 whitefish" "6 bluefish"
[41] "7 redfish" "8 whitefish" "9 bluefish" "10 redfish"
[45] "11 whitefish" "12 bluefish" "13 redfish" "14 whitefish"
[49] "15 bluefish" "16 redfish" "17 whitefish" "18 bluefish"
[53] "19 redfish" "20 whitefish" "21 bluefish" "22 redfish"
[57] "23 whitefish" "24 bluefish" "25 redfish" "26 whitefish"
[61] "27 bluefish" "28 redfish" "29 whitefish" "30 bluefish"
[65] "31 redfish" "32 whitefish" "33 bluefish" "34 redfish"
[69] "35 whitefish" "36 bluefish" "2 redfish" "3 whitefish"
[73] "4 bluefish" "5 redfish" "6 whitefish" "7 bluefish"
[77] "8 redfish" "9 whitefish" "10 bluefish" "11 redfish"
[81] "12 whitefish" "13 bluefish" "14 redfish" "15 whitefish"
[85] "16 bluefish" "17 redfish" "18 whitefish" "19 bluefish"
[89] "20 redfish" "21 whitefish" "22 bluefish" "23 redfish"
[93] "24 whitefish" "25 bluefish" "26 redfish" "27 whitefish"
[97] "28 bluefish" "29 redfish" "30 whitefish" "31 bluefish"
[101] "32 redfish" "33 whitefish" "34 bluefish" "35 redfish"
[105] "36 whitefish"
-b)
b1 <- rpois(100,10)
b1
[1] 12 7 12 7 17 8 9 6 10 7 12 10 9 12 7 11 13 1 11 11 13
[22] 3 8 14 9 6 10 13 13 8 11 10 6 7 8 14 6 12 5 8 7 10
[43] 8 12 6 13 7 10 8 6 4 10 11 15 20 12 13 14 10 13 9 9 9
[64] 12 12 14 12 9 6 12 9 11 7 12 12 7 8 14 5 9 12 13 11 6
[85] 11 17 11 5 3 9 18 12 7 7 8 5 9 11 12 12
b2 <- b1[b1 == 8]
table(b2)
b2
8
9
length(b2)
[1] 9
b3 <- b1[b1 >10]
b3
[1] 12 12 17 12 12 11 13 11 11 13 14 13 13 11 14 12 12 13 11 15 20 12
[23] 13 14 13 12 12 14 12 12 11 12 12 14 12 13 11 11 17 11 18 12 11 12
[45] 12
b4 <- b1[b1 <10 & b1 >5]
b4
[1] 7 7 8 9 6 7 9 7 8 9 6 8 6 7 8 6 8 7 8 6 7 8 6 9 9 9 9 6 9 7 7 8 9
[34] 6 9 7 7 8 9
c1 <- matrix(1:20, nrow = 5, byrow = TRUE)
c1
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[5,] 17 18 19 20
-a)
[1] 7 15 10 10 13 17
[1] 0.048
[1] 23 20 20 19 18 18
[1] 1.945910 2.708050 2.302585 2.302585 2.564949 2.833213
[1] 7 15 10 10 13 17 14 12 12 0 9 9 10 13 9 6 7 10 13 0 13
[22] 9 16 8 15 11 12 8 9 0 11 7 15 5 9 12 11 8 6 0 9 7
[43] 15 10 7 9 9 9 15 0 8 9 9 11 9 12 7 10 17 0 16 13 7
[64] 14 16 11 7 7 13 0 16 10 10 5 14 4 14 3 9 0 10 11 16 3
[85] 6 9 7 8 8 0 9 8 10 7 8 9 7 7 8 0 15 11 14 19 9
[106] 6 10 6 6 0 6 5 13 5 15 6 9 10 9 0 9 6 6 9 9 6
[127] 9 9 4 0 10 6 10 11 9 11 13 12 9 0 6 10 14 7 10 6 8
[148] 10 7 0 7 8 6 11 12 5 15 9 10 0 6 10 9 14 9 10 9 11
[169] 14 0 9 8 7 10 11 7 12 12 15 0 14 11 12 9 8 8 10 9 8
[190] 0 9 10 11 7 17 4 6 18 13 0 14 8 9 7 14 12 11 11 10 0
[211] 12 8 6 10 17 9 12 9 14 0 8 6 13 11 9 12 8 17 7 0 14
[232] 10 14 6 13 13 10 11 12 0 7 8 6 15 11 5 11 11 13 0 7 9
[253] 10 12 13 20 9 12 16 0 11 5 9 14 8 10 10 8 10 0 15 12 9
[274] 10 13 10 13 8 11 0 9 9 9 12 8 10 7 8 11 0 11 15 12 10
[295] 18 10 11 11 5 0 7 8 12 13 7 14 11 14 12 0 7 8 14 13 15
[316] 9 11 10 10 0 10 14 12 13 12 17 10 9 13 0 10 12 5 9 5 10
[337] 12 15 10 0 13 7 23 8 12 10 10 15 13 0 13 15 14 10 15 7 4
[358] 11 8 0 13 8 6 18 13 11 5 16 11 0 12 8 13 9 9 15 11 5
[379] 8 0 10 6 11 13 9 12 7 9 15 0 10 11 10 10 12 18 11 10 14
[400] 0 3 13 7 8 12 3 20 7 7 0 9 10 7 11 7 8 15 7 15 0
[421] 10 2 9 10 9 10 11 12 13 0 9 6 14 8 14 15 16 12 14 0 10
[442] 7 1 9 12 12 8 5 11 0 6 16 11 13 10 14 11 11 8 0 11 9
[463] 9 11 9 11 4 12 10 0 15 8 7 9 8 12 11 13 13 0 9 13 12
[484] 11 7 11 14 10 13 0 5 9 5 12 11 11 14 10 13 0
-b)
b1 <- matrix(c(seq(from = 0, to = 33, by = 3), seq(from = 24, to = 2, by = -2)), ncol = 4, byrow = TRUE)
rownames(b1) <- c("S1", "S2", "S3", "S4", "S5", "S6")
colnames(b1) <- c("redfish", "bluefish", "whitefish", "yellowfish")
b2 <- as.data.frame(b1) # turn into data frame
b2$Location <- c(rep(c("Texas", "Belize"), times = 3)) # add new column
b3 <- as.matrix(b2[3,c(1:4)]) # turn row 3 and columns 1:4 back into a matrix
mean(b3) # take the mean of the matrix
[1] 28.5
[1] 14.5
[1] 15.5
print(b4difference <- b4blue - b4yellow) # difference
[1] -1
-c)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 97.21767 98.19251 93.45351 89.30902 101.48067 85.42450
[2,] 112.99639 112.76412 84.48413 118.81134 103.40519 81.40670
[3,] 106.45122 119.29482 94.46852 102.80781 108.28285 82.31760
[4,] 97.25306 86.76533 108.07064 105.26943 97.04863 109.94568
[5,] 104.41281 97.21637 106.48560 78.29566 101.93544 112.42794
[6,] 94.24697 84.24974 113.55471 109.67638 92.92682 97.39542
[,7] [,8] [,9] [,10] [,11] [,12]
[1,] 122.58092 93.95745 109.08302 108.11612 95.05754 87.91288
[2,] 103.40033 113.22225 104.07990 86.97251 103.88598 96.43647
[3,] 92.12816 104.02716 99.04348 99.30539 98.11165 96.91853
[4,] 109.72486 109.90308 92.09482 114.19912 95.20120 112.57355
[5,] 109.10043 102.44735 104.95061 86.14095 99.82553 112.21784
[6,] 97.95533 103.50476 110.38871 94.10369 95.68369 102.20491
[,13] [,14] [,15] [,16] [,17] [,18]
[1,] 100.65613 104.29737 98.61514 112.52038 105.34923 97.80894
[2,] 84.19144 107.08536 92.61325 112.71201 85.42717 120.30674
[3,] 87.59011 95.12201 98.32900 119.76084 99.52681 101.18667
[4,] 105.11519 108.86852 105.44996 101.44141 108.39526 112.43779
[5,] 105.29518 88.91743 104.73334 88.17577 103.43581 100.49787
[6,] 112.98843 107.75935 92.63203 97.43995 92.39601 91.03433
[,19] [,20]
[1,] 104.97778 95.40465
[2,] 103.55934 112.22857
[3,] 106.81857 91.54977
[4,] 79.84788 122.38293
[5,] 83.26464 98.83141
[6,] 99.76014 116.80139
c2 <- c1[98, 5] # look for value in row 98 column 5
c2
[1] 87.66383
summary(c1) # look for maximum value and largest deviation from the specified mean (100)
V1 V2 V3 V4
Min. : 72.70 Min. : 72.53 Min. : 80.57 Min. : 78.30
1st Qu.: 92.89 1st Qu.: 93.38 1st Qu.: 95.36 1st Qu.: 91.04
Median : 99.45 Median : 98.22 Median :102.07 Median : 98.11
Mean :100.50 Mean : 98.82 Mean :101.54 Mean : 98.35
3rd Qu.:106.90 3rd Qu.:105.01 3rd Qu.:106.90 3rd Qu.:105.09
Max. :127.57 Max. :126.43 Max. :127.18 Max. :125.37
V5 V6 V7 V8
Min. : 84.67 Min. : 76.21 Min. : 81.34 Min. : 71.72
1st Qu.: 94.21 1st Qu.: 88.71 1st Qu.: 95.66 1st Qu.: 93.95
Median :100.78 Median : 97.27 Median :102.19 Median :100.67
Mean :101.06 Mean : 98.51 Mean :101.64 Mean :100.33
3rd Qu.:106.08 3rd Qu.:107.31 3rd Qu.:108.66 3rd Qu.:106.66
Max. :125.26 Max. :124.84 Max. :127.11 Max. :119.85
V9 V10 V11 V12
Min. : 80.07 Min. : 78.39 Min. : 77.27 Min. : 69.94
1st Qu.: 95.15 1st Qu.: 91.83 1st Qu.: 93.18 1st Qu.: 92.57
Median :101.82 Median : 99.34 Median : 99.59 Median :100.19
Mean :101.63 Mean : 99.14 Mean :100.12 Mean : 99.34
3rd Qu.:109.15 3rd Qu.:105.13 3rd Qu.:105.83 3rd Qu.:106.09
Max. :129.19 Max. :137.78 Max. :126.83 Max. :125.07
V13 V14 V15 V16
Min. : 67.65 Min. : 77.26 Min. : 78.14 Min. : 78.45
1st Qu.: 91.77 1st Qu.: 92.62 1st Qu.: 94.66 1st Qu.: 93.40
Median : 99.78 Median :100.77 Median :100.36 Median :101.48
Mean : 98.99 Mean :100.36 Mean :101.11 Mean :100.34
3rd Qu.:106.93 3rd Qu.:106.65 3rd Qu.:108.66 3rd Qu.:107.24
Max. :121.45 Max. :121.23 Max. :119.55 Max. :120.02
V17 V18 V19 V20
Min. : 76.08 Min. : 75.54 Min. : 65.81 Min. : 71.11
1st Qu.: 93.85 1st Qu.: 94.04 1st Qu.: 95.15 1st Qu.: 94.95
Median : 99.28 Median : 99.27 Median :101.63 Median :102.04
Mean : 99.40 Mean : 99.61 Mean :100.62 Mean :100.60
3rd Qu.:104.70 3rd Qu.:104.31 3rd Qu.:106.71 3rd Qu.:107.50
Max. :120.81 Max. :124.68 Max. :122.01 Max. :122.38
-d)
herbivores <- read.csv(file = "data/coralreefherbivores.csv")
herbivore.tbl <- dplyr::as_tibble(herbivores) # turn into tibble - might need a new package here ;-)
herbivore.tbl # tibbles tell you the data classes but str() would also work well
# A tibble: 96 × 10
family genus species gen.spe sl bodydepth snoutlength
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 Acanthuridae Acanthurus achill… Acanth… 164. 0.554 0.488
2 Acanthuridae Acanthurus albipe… Acanth… 213. 0.441 0.440
3 Acanthuridae Acanthurus aurant… Acanth… 216 0.473 0.539
4 Acanthuridae Acanthurus blochii Acanth… 82.9 0.559 0.478
5 Acanthuridae Acanthurus dussum… Acanth… 194. 0.546 0.566
6 Acanthuridae Acanthurus fowleri Acanth… 266 0.467 0.595
7 Acanthuridae Acanthurus guttat… Acanth… 150 0.680 0.404
8 Acanthuridae Acanthurus leucoc… Acanth… 229. 0.491 0.563
9 Acanthuridae Acanthurus leucop… Acanth… 190. 0.578 0.490
10 Acanthuridae Acanthurus lineat… Acanth… 133. 0.499 0.464
# ℹ 86 more rows
# ℹ 3 more variables: eyediameter <dbl>, size <chr>, schooling <chr>
herbivore.tbl$gen.spe # the two species are rows 31 and 96
[1] "Acanthurus.achilles" "Acanthurus.albipectoralis"
[3] "Acanthurus.auranticavus" "Acanthurus.blochii"
[5] "Acanthurus.dussumieri" "Acanthurus.fowleri"
[7] "Acanthurus.guttatus" "Acanthurus.leucocheilus"
[9] "Acanthurus.leucopareius" "Acanthurus.lineatus"
[11] "Acanthurus.maculiceps" "Acanthurus.mata"
[13] "Acanthurus.nigricans" "Acanthurus.nigricauda"
[15] "Acanthurus.nigrofuscus" "Acanthurus.nigroris"
[17] "Acanthurus.nubilus" "Acanthurus.olivaceus"
[19] "Acanthurus.pyroferus" "Acanthurus.thompsoni"
[21] "Acanthurus.triostegus" "Acanthurus.xanthopterus"
[23] "Bolbometopon.muricatum" "Calotomus.carolinus"
[25] "Calotomus.spinidens" "Cetoscarus.ocellatus"
[27] "Chlorurus.bleekeri" "Chlorurus.bowersi"
[29] "Chlorurus.frontalis" "Chlorurus.japanensis"
[31] "Chlorurus.microrhinos" "Ctenochaetus.binotatus"
[33] "Ctenochaetus.flavicauda" "Ctenochaetus.hawaiiensis"
[35] "Ctenochaetus.marginatus" "Ctenochaetus.striatus"
[37] "Ctenochaetus.strigosus" "Ctenochaetus.tominiensis"
[39] "Hipposcarus.longiceps" "Kyphosus.vaigiensis"
[41] "Leptoscarus.vaigiensis" "Naso.annulatus"
[43] "Naso.brachycentron" "Naso.brevirostris"
[45] "Naso.caesius" "Naso.hexacanthus"
[47] "Naso.lituratus" "Naso.lopezi"
[49] "Naso.thynnoides" "Naso.tuberosus"
[51] "Naso.unicornis" "Naso.vlamingii"
[53] "Paracanthurus.hepatus" "Scarus.altipinnis"
[55] "Scarus.chameleon" "Scarus.dimidiatus"
[57] "Scarus.festivus" "Scarus.flavipectoralis"
[59] "Scarus.forsteni" "Scarus.frenatus"
[61] "Scarus.ghobban" "Scarus.globiceps"
[63] "Scarus.hypselopterus" "Scarus.longipinnis"
[65] "Scarus.niger" "Scarus.oviceps"
[67] "Scarus.prasiognathos" "Scarus.psittacus"
[69] "Scarus.quoyi" "Scarus.rivulatus"
[71] "Scarus.rubroviolaceus" "Scarus.schlegeli"
[73] "Scarus.spinus" "Scarus.tricolor"
[75] "Scarus.xanthopleura" "Siganus.argenteus"
[77] "Siganus.canaliculatus" "Siganus.corallinus"
[79] "Siganus.doliatus" "Siganus.fuscescens"
[81] "Siganus.guttatus" "Siganus.javus"
[83] "Siganus.lineatus" "Siganus.niger"
[85] "Siganus.puellus" "Siganus.punctatissimus"
[87] "Siganus.punctatus" "Siganus.randalli"
[89] "Siganus.spinus" "Siganus.stellatus"
[91] "Siganus.vermiculatus" "Siganus.vulpinus"
[93] "Zebrasoma.flavescens" "Zebrasoma.rostratum"
[95] "Zebrasoma.scopas" "Zebrasoma.velifer"
herbivore.tbl[c(31,96),] # check snoutlength
# A tibble: 2 × 10
family genus species gen.spe sl bodydepth snoutlength eyediameter
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Labri… Chlo… micror… Chloru… 417 0.402 0.359 0.149
2 Acant… Zebr… velifer Zebras… 153 0.548 0.625 0.359
# ℹ 2 more variables: size <chr>, schooling <chr>
If you see mistakes or want to suggest changes, please create an issue on the source repository.
Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. Source code is available at https://github.com/simonjbrandl/marinecommunityecology, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".