The following is in response to our 2nd week’s assignment. Using the provided list of functions I will define each and demonstrate the function in use.

General Bits

  1. help(topic) – opens the help menu to the R Documentation on topic written in the parantheses
help(plot)
  1. ?topic – similar to help(topic), opens R Documentation
?distribution
  1. ls() – shows defined variables
ls()
## character(0)
  1. dir() – shows files in directory
dir()
##  [1] "_site.yml"               "docs"                   
##  [3] "images"                  "index.Rmd"              
##  [5] "Intrinsics_cache"        "Intrinsics.Rmd"         
##  [7] "Journal_cache"           "Journal_files"          
##  [9] "Journal.Rmd"             "LabJournalWebsite.Rproj"
## [11] "Links.Rmd"               "README.md"
  1. list.files() – lists the files in a directory/folder
list.files()
##  [1] "_site.yml"               "docs"                   
##  [3] "images"                  "index.Rmd"              
##  [5] "Intrinsics_cache"        "Intrinsics.Rmd"         
##  [7] "Journal_cache"           "Journal_files"          
##  [9] "Journal.Rmd"             "LabJournalWebsite.Rproj"
## [11] "Links.Rmd"               "README.md"

Input and Output

  1. save() – function to save data created in R in a specific file
red<- seq(1,20,2)
save(red,file="seq.rmd")
  1. load() – reloads objects or datasets that were configured with save()
load("seq.rmd")
  1. data() – lists the data sets currently known or saved, loads specificed data sets
data("iris")
  1. library() – lists the packages in R’s library and can load specified packages in library
library(ggplot2)
  1. read.table() – opens a document or file as a table
read.table(file="int_prac.rtf",sep=",")
##                                                                                                                                         V1
## 1                                                                                 {\\rtf1\\ansi\\ansicpg1252\\cocoartf1671\\cocoasubrtf200
## 2                                                                                            {\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}
## 3                                                                                                {\\colortbl;\\red255\\green255\\blue255;}
## 4                                                                                                                {\\*\\expandedcolortbl;;}
## 5                                                                                 \\margl1440\\margr1440\\vieww10800\\viewh8400\\viewkind0
## 6  \\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\pardirnatural\\partightenfactor0
## 7                                                                                                                    \\f0\\fs24 \\cf0 Name
## 8                                                                                                                                    Age\\
## 9  \\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\pardirnatural\\partightenfactor0
## 10                                                                                                                               \\cf0 Ana
## 11                                                                                                                                    23\\
## 12                                                                                                                                    Fred
## 13                                                                                                                                    34\\
## 14                                                                                                                                     Dan
## 15                                                                                                                                    62\\
## 16                                                                                                                                   David
## 17                                                                                                                                     12}

– did not execute properly, don’t know what wrong.

  1. read.csv() – function to read a csv file and convert to R
read.csv(file="iris.csv", header=TRUE, sep="")
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1            5.1         3.5          1.4         0.2     setosa
## 2            4.9         3.0          1.4         0.2     setosa
## 3            4.7         3.2          1.3         0.2     setosa
## 4            4.6         3.1          1.5         0.2     setosa
## 5            5.0         3.6          1.4         0.2     setosa
## 6            5.4         3.9          1.7         0.4     setosa
## 7            4.6         3.4          1.4         0.3     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 9            4.4         2.9          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 14           4.3         3.0          1.1         0.1     setosa
## 15           5.8         4.0          1.2         0.2     setosa
## 16           5.7         4.4          1.5         0.4     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 34           5.5         4.2          1.4         0.2     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 39           4.4         3.0          1.3         0.2     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 42           4.5         2.3          1.3         0.3     setosa
## 43           4.4         3.2          1.3         0.2     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 49           5.3         3.7          1.5         0.2     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 51           7.0         3.2          4.7         1.4 versicolor
## 52           6.4         3.2          4.5         1.5 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 54           5.5         2.3          4.0         1.3 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 56           5.7         2.8          4.5         1.3 versicolor
## 57           6.3         3.3          4.7         1.6 versicolor
## 58           4.9         2.4          3.3         1.0 versicolor
## 59           6.6         2.9          4.6         1.3 versicolor
## 60           5.2         2.7          3.9         1.4 versicolor
## 61           5.0         2.0          3.5         1.0 versicolor
## 62           5.9         3.0          4.2         1.5 versicolor
## 63           6.0         2.2          4.0         1.0 versicolor
## 64           6.1         2.9          4.7         1.4 versicolor
## 65           5.6         2.9          3.6         1.3 versicolor
## 66           6.7         3.1          4.4         1.4 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 68           5.8         2.7          4.1         1.0 versicolor
## 69           6.2         2.2          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 77           6.8         2.8          4.8         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 85           5.4         3.0          4.5         1.5 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 99           5.1         2.5          3.0         1.1 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 102          5.8         2.7          5.1         1.9  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 105          6.5         3.0          5.8         2.2  virginica
## 106          7.6         3.0          6.6         2.1  virginica
## 107          4.9         2.5          4.5         1.7  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 109          6.7         2.5          5.8         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 112          6.4         2.7          5.3         1.9  virginica
## 113          6.8         3.0          5.5         2.1  virginica
## 114          5.7         2.5          5.0         2.0  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 118          7.7         3.8          6.7         2.2  virginica
## 119          7.7         2.6          6.9         2.3  virginica
## 120          6.0         2.2          5.0         1.5  virginica
## 121          6.9         3.2          5.7         2.3  virginica
## 122          5.6         2.8          4.9         2.0  virginica
## 123          7.7         2.8          6.7         2.0  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 127          6.2         2.8          4.8         1.8  virginica
## 128          6.1         3.0          4.9         1.8  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 131          7.4         2.8          6.1         1.9  virginica
## 132          7.9         3.8          6.4         2.0  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 136          7.7         3.0          6.1         2.3  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 145          6.7         3.3          5.7         2.5  virginica
## 146          6.7         3.0          5.2         2.3  virginica
## 147          6.3         2.5          5.0         1.9  virginica
## 148          6.5         3.0          5.2         2.0  virginica
## 149          6.2         3.4          5.4         2.3  virginica
## 150          5.9         3.0          5.1         1.8  virginica
  1. scan() – scans a file (??)
scan(file = "iris.csv", what = list(""))
## [[1]]
##   [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
##   [5] "Species"      "1"            "5.1"          "3.5"         
##   [9] "1.4"          "0.2"          "setosa"       "2"           
##  [13] "4.9"          "3"            "1.4"          "0.2"         
##  [17] "setosa"       "3"            "4.7"          "3.2"         
##  [21] "1.3"          "0.2"          "setosa"       "4"           
##  [25] "4.6"          "3.1"          "1.5"          "0.2"         
##  [29] "setosa"       "5"            "5"            "3.6"         
##  [33] "1.4"          "0.2"          "setosa"       "6"           
##  [37] "5.4"          "3.9"          "1.7"          "0.4"         
##  [41] "setosa"       "7"            "4.6"          "3.4"         
##  [45] "1.4"          "0.3"          "setosa"       "8"           
##  [49] "5"            "3.4"          "1.5"          "0.2"         
##  [53] "setosa"       "9"            "4.4"          "2.9"         
##  [57] "1.4"          "0.2"          "setosa"       "10"          
##  [61] "4.9"          "3.1"          "1.5"          "0.1"         
##  [65] "setosa"       "11"           "5.4"          "3.7"         
##  [69] "1.5"          "0.2"          "setosa"       "12"          
##  [73] "4.8"          "3.4"          "1.6"          "0.2"         
##  [77] "setosa"       "13"           "4.8"          "3"           
##  [81] "1.4"          "0.1"          "setosa"       "14"          
##  [85] "4.3"          "3"            "1.1"          "0.1"         
##  [89] "setosa"       "15"           "5.8"          "4"           
##  [93] "1.2"          "0.2"          "setosa"       "16"          
##  [97] "5.7"          "4.4"          "1.5"          "0.4"         
## [101] "setosa"       "17"           "5.4"          "3.9"         
## [105] "1.3"          "0.4"          "setosa"       "18"          
## [109] "5.1"          "3.5"          "1.4"          "0.3"         
## [113] "setosa"       "19"           "5.7"          "3.8"         
## [117] "1.7"          "0.3"          "setosa"       "20"          
## [121] "5.1"          "3.8"          "1.5"          "0.3"         
## [125] "setosa"       "21"           "5.4"          "3.4"         
## [129] "1.7"          "0.2"          "setosa"       "22"          
## [133] "5.1"          "3.7"          "1.5"          "0.4"         
## [137] "setosa"       "23"           "4.6"          "3.6"         
## [141] "1"            "0.2"          "setosa"       "24"          
## [145] "5.1"          "3.3"          "1.7"          "0.5"         
## [149] "setosa"       "25"           "4.8"          "3.4"         
## [153] "1.9"          "0.2"          "setosa"       "26"          
## [157] "5"            "3"            "1.6"          "0.2"         
## [161] "setosa"       "27"           "5"            "3.4"         
## [165] "1.6"          "0.4"          "setosa"       "28"          
## [169] "5.2"          "3.5"          "1.5"          "0.2"         
## [173] "setosa"       "29"           "5.2"          "3.4"         
## [177] "1.4"          "0.2"          "setosa"       "30"          
## [181] "4.7"          "3.2"          "1.6"          "0.2"         
## [185] "setosa"       "31"           "4.8"          "3.1"         
## [189] "1.6"          "0.2"          "setosa"       "32"          
## [193] "5.4"          "3.4"          "1.5"          "0.4"         
## [197] "setosa"       "33"           "5.2"          "4.1"         
## [201] "1.5"          "0.1"          "setosa"       "34"          
## [205] "5.5"          "4.2"          "1.4"          "0.2"         
## [209] "setosa"       "35"           "4.9"          "3.1"         
## [213] "1.5"          "0.2"          "setosa"       "36"          
## [217] "5"            "3.2"          "1.2"          "0.2"         
## [221] "setosa"       "37"           "5.5"          "3.5"         
## [225] "1.3"          "0.2"          "setosa"       "38"          
## [229] "4.9"          "3.6"          "1.4"          "0.1"         
## [233] "setosa"       "39"           "4.4"          "3"           
## [237] "1.3"          "0.2"          "setosa"       "40"          
## [241] "5.1"          "3.4"          "1.5"          "0.2"         
## [245] "setosa"       "41"           "5"            "3.5"         
## [249] "1.3"          "0.3"          "setosa"       "42"          
## [253] "4.5"          "2.3"          "1.3"          "0.3"         
## [257] "setosa"       "43"           "4.4"          "3.2"         
## [261] "1.3"          "0.2"          "setosa"       "44"          
## [265] "5"            "3.5"          "1.6"          "0.6"         
## [269] "setosa"       "45"           "5.1"          "3.8"         
## [273] "1.9"          "0.4"          "setosa"       "46"          
## [277] "4.8"          "3"            "1.4"          "0.3"         
## [281] "setosa"       "47"           "5.1"          "3.8"         
## [285] "1.6"          "0.2"          "setosa"       "48"          
## [289] "4.6"          "3.2"          "1.4"          "0.2"         
## [293] "setosa"       "49"           "5.3"          "3.7"         
## [297] "1.5"          "0.2"          "setosa"       "50"          
## [301] "5"            "3.3"          "1.4"          "0.2"         
## [305] "setosa"       "51"           "7"            "3.2"         
## [309] "4.7"          "1.4"          "versicolor"   "52"          
## [313] "6.4"          "3.2"          "4.5"          "1.5"         
## [317] "versicolor"   "53"           "6.9"          "3.1"         
## [321] "4.9"          "1.5"          "versicolor"   "54"          
## [325] "5.5"          "2.3"          "4"            "1.3"         
## [329] "versicolor"   "55"           "6.5"          "2.8"         
## [333] "4.6"          "1.5"          "versicolor"   "56"          
## [337] "5.7"          "2.8"          "4.5"          "1.3"         
## [341] "versicolor"   "57"           "6.3"          "3.3"         
## [345] "4.7"          "1.6"          "versicolor"   "58"          
## [349] "4.9"          "2.4"          "3.3"          "1"           
## [353] "versicolor"   "59"           "6.6"          "2.9"         
## [357] "4.6"          "1.3"          "versicolor"   "60"          
## [361] "5.2"          "2.7"          "3.9"          "1.4"         
## [365] "versicolor"   "61"           "5"            "2"           
## [369] "3.5"          "1"            "versicolor"   "62"          
## [373] "5.9"          "3"            "4.2"          "1.5"         
## [377] "versicolor"   "63"           "6"            "2.2"         
## [381] "4"            "1"            "versicolor"   "64"          
## [385] "6.1"          "2.9"          "4.7"          "1.4"         
## [389] "versicolor"   "65"           "5.6"          "2.9"         
## [393] "3.6"          "1.3"          "versicolor"   "66"          
## [397] "6.7"          "3.1"          "4.4"          "1.4"         
## [401] "versicolor"   "67"           "5.6"          "3"           
## [405] "4.5"          "1.5"          "versicolor"   "68"          
## [409] "5.8"          "2.7"          "4.1"          "1"           
## [413] "versicolor"   "69"           "6.2"          "2.2"         
## [417] "4.5"          "1.5"          "versicolor"   "70"          
## [421] "5.6"          "2.5"          "3.9"          "1.1"         
## [425] "versicolor"   "71"           "5.9"          "3.2"         
## [429] "4.8"          "1.8"          "versicolor"   "72"          
## [433] "6.1"          "2.8"          "4"            "1.3"         
## [437] "versicolor"   "73"           "6.3"          "2.5"         
## [441] "4.9"          "1.5"          "versicolor"   "74"          
## [445] "6.1"          "2.8"          "4.7"          "1.2"         
## [449] "versicolor"   "75"           "6.4"          "2.9"         
## [453] "4.3"          "1.3"          "versicolor"   "76"          
## [457] "6.6"          "3"            "4.4"          "1.4"         
## [461] "versicolor"   "77"           "6.8"          "2.8"         
## [465] "4.8"          "1.4"          "versicolor"   "78"          
## [469] "6.7"          "3"            "5"            "1.7"         
## [473] "versicolor"   "79"           "6"            "2.9"         
## [477] "4.5"          "1.5"          "versicolor"   "80"          
## [481] "5.7"          "2.6"          "3.5"          "1"           
## [485] "versicolor"   "81"           "5.5"          "2.4"         
## [489] "3.8"          "1.1"          "versicolor"   "82"          
## [493] "5.5"          "2.4"          "3.7"          "1"           
## [497] "versicolor"   "83"           "5.8"          "2.7"         
## [501] "3.9"          "1.2"          "versicolor"   "84"          
## [505] "6"            "2.7"          "5.1"          "1.6"         
## [509] "versicolor"   "85"           "5.4"          "3"           
## [513] "4.5"          "1.5"          "versicolor"   "86"          
## [517] "6"            "3.4"          "4.5"          "1.6"         
## [521] "versicolor"   "87"           "6.7"          "3.1"         
## [525] "4.7"          "1.5"          "versicolor"   "88"          
## [529] "6.3"          "2.3"          "4.4"          "1.3"         
## [533] "versicolor"   "89"           "5.6"          "3"           
## [537] "4.1"          "1.3"          "versicolor"   "90"          
## [541] "5.5"          "2.5"          "4"            "1.3"         
## [545] "versicolor"   "91"           "5.5"          "2.6"         
## [549] "4.4"          "1.2"          "versicolor"   "92"          
## [553] "6.1"          "3"            "4.6"          "1.4"         
## [557] "versicolor"   "93"           "5.8"          "2.6"         
## [561] "4"            "1.2"          "versicolor"   "94"          
## [565] "5"            "2.3"          "3.3"          "1"           
## [569] "versicolor"   "95"           "5.6"          "2.7"         
## [573] "4.2"          "1.3"          "versicolor"   "96"          
## [577] "5.7"          "3"            "4.2"          "1.2"         
## [581] "versicolor"   "97"           "5.7"          "2.9"         
## [585] "4.2"          "1.3"          "versicolor"   "98"          
## [589] "6.2"          "2.9"          "4.3"          "1.3"         
## [593] "versicolor"   "99"           "5.1"          "2.5"         
## [597] "3"            "1.1"          "versicolor"   "100"         
## [601] "5.7"          "2.8"          "4.1"          "1.3"         
## [605] "versicolor"   "101"          "6.3"          "3.3"         
## [609] "6"            "2.5"          "virginica"    "102"         
## [613] "5.8"          "2.7"          "5.1"          "1.9"         
## [617] "virginica"    "103"          "7.1"          "3"           
## [621] "5.9"          "2.1"          "virginica"    "104"         
## [625] "6.3"          "2.9"          "5.6"          "1.8"         
## [629] "virginica"    "105"          "6.5"          "3"           
## [633] "5.8"          "2.2"          "virginica"    "106"         
## [637] "7.6"          "3"            "6.6"          "2.1"         
## [641] "virginica"    "107"          "4.9"          "2.5"         
## [645] "4.5"          "1.7"          "virginica"    "108"         
## [649] "7.3"          "2.9"          "6.3"          "1.8"         
## [653] "virginica"    "109"          "6.7"          "2.5"         
## [657] "5.8"          "1.8"          "virginica"    "110"         
## [661] "7.2"          "3.6"          "6.1"          "2.5"         
## [665] "virginica"    "111"          "6.5"          "3.2"         
## [669] "5.1"          "2"            "virginica"    "112"         
## [673] "6.4"          "2.7"          "5.3"          "1.9"         
## [677] "virginica"    "113"          "6.8"          "3"           
## [681] "5.5"          "2.1"          "virginica"    "114"         
## [685] "5.7"          "2.5"          "5"            "2"           
## [689] "virginica"    "115"          "5.8"          "2.8"         
## [693] "5.1"          "2.4"          "virginica"    "116"         
## [697] "6.4"          "3.2"          "5.3"          "2.3"         
## [701] "virginica"    "117"          "6.5"          "3"           
## [705] "5.5"          "1.8"          "virginica"    "118"         
## [709] "7.7"          "3.8"          "6.7"          "2.2"         
## [713] "virginica"    "119"          "7.7"          "2.6"         
## [717] "6.9"          "2.3"          "virginica"    "120"         
## [721] "6"            "2.2"          "5"            "1.5"         
## [725] "virginica"    "121"          "6.9"          "3.2"         
## [729] "5.7"          "2.3"          "virginica"    "122"         
## [733] "5.6"          "2.8"          "4.9"          "2"           
## [737] "virginica"    "123"          "7.7"          "2.8"         
## [741] "6.7"          "2"            "virginica"    "124"         
## [745] "6.3"          "2.7"          "4.9"          "1.8"         
## [749] "virginica"    "125"          "6.7"          "3.3"         
## [753] "5.7"          "2.1"          "virginica"    "126"         
## [757] "7.2"          "3.2"          "6"            "1.8"         
## [761] "virginica"    "127"          "6.2"          "2.8"         
## [765] "4.8"          "1.8"          "virginica"    "128"         
## [769] "6.1"          "3"            "4.9"          "1.8"         
## [773] "virginica"    "129"          "6.4"          "2.8"         
## [777] "5.6"          "2.1"          "virginica"    "130"         
## [781] "7.2"          "3"            "5.8"          "1.6"         
## [785] "virginica"    "131"          "7.4"          "2.8"         
## [789] "6.1"          "1.9"          "virginica"    "132"         
## [793] "7.9"          "3.8"          "6.4"          "2"           
## [797] "virginica"    "133"          "6.4"          "2.8"         
## [801] "5.6"          "2.2"          "virginica"    "134"         
## [805] "6.3"          "2.8"          "5.1"          "1.5"         
## [809] "virginica"    "135"          "6.1"          "2.6"         
## [813] "5.6"          "1.4"          "virginica"    "136"         
## [817] "7.7"          "3"            "6.1"          "2.3"         
## [821] "virginica"    "137"          "6.3"          "3.4"         
## [825] "5.6"          "2.4"          "virginica"    "138"         
## [829] "6.4"          "3.1"          "5.5"          "1.8"         
## [833] "virginica"    "139"          "6"            "3"           
## [837] "4.8"          "1.8"          "virginica"    "140"         
## [841] "6.9"          "3.1"          "5.4"          "2.1"         
## [845] "virginica"    "141"          "6.7"          "3.1"         
## [849] "5.6"          "2.4"          "virginica"    "142"         
## [853] "6.9"          "3.1"          "5.1"          "2.3"         
## [857] "virginica"    "143"          "5.8"          "2.7"         
## [861] "5.1"          "1.9"          "virginica"    "144"         
## [865] "6.8"          "3.2"          "5.9"          "2.3"         
## [869] "virginica"    "145"          "6.7"          "3.3"         
## [873] "5.7"          "2.5"          "virginica"    "146"         
## [877] "6.7"          "3"            "5.2"          "2.3"         
## [881] "virginica"    "147"          "6.3"          "2.5"         
## [885] "5"            "1.9"          "virginica"    "148"         
## [889] "6.5"          "3"            "5.2"          "2"           
## [893] "virginica"    "149"          "6.2"          "3.4"         
## [897] "5.4"          "2.3"          "virginica"    "150"         
## [901] "5.9"          "3"            "5.1"          "1.8"         
## [905] "virginica"
  1. print() – prints the content of specificed variable onto screen
ans<- 5*8
print(ans)
## [1] 40
  1. cat() – concatenates two separate strings
AL <-cat("ana",".","lakshin")
## ana . lakshin
  1. write.table() – exports R data as a readable and downloadable file
write.table(iris,file="iris.csv",row.names = TRUE)

Data Creation

  1. c() – combines a vector so that a single variable could be assigned a set of numbers
abs<- c(2,6,9,12)
  1. from:to (where from and to are replaced with numbers, e.g. 1:10) – create a sequence of numbers
bc <- 12:4
cd <- bc + 2
2:5 *2
## [1]  4  6  8 10
  1. seq() – function to create a sequence of numbers
sam<- seq(4,18,.25)
print(sam)
##  [1]  4.00  4.25  4.50  4.75  5.00  5.25  5.50  5.75  6.00  6.25  6.50
## [12]  6.75  7.00  7.25  7.50  7.75  8.00  8.25  8.50  8.75  9.00  9.25
## [23]  9.50  9.75 10.00 10.25 10.50 10.75 11.00 11.25 11.50 11.75 12.00
## [34] 12.25 12.50 12.75 13.00 13.25 13.50 13.75 14.00 14.25 14.50 14.75
## [45] 15.00 15.25 15.50 15.75 16.00 16.25 16.50 16.75 17.00 17.25 17.50
## [56] 17.75 18.00
  1. rep() – replicates values in the specified variable
rep(6:9, times=2)
## [1] 6 7 8 9 6 7 8 9
  1. data.frame() – converts matrices into data frames, creates data frames
yan <- matrix(seq(1,24,1), nrow=6, ncol = 4)
data.frame(yan)
##   X1 X2 X3 X4
## 1  1  7 13 19
## 2  2  8 14 20
## 3  3  9 15 21
## 4  4 10 16 22
## 5  5 11 17 23
## 6  6 12 18 24
  1. list() – creates a list
kin<- list("a"=2.5, "b"= TRUE, "c"= 2:5)

#howdo you do this with names?
  1. matrix() – creates a martix with specified rows and columns
yan <- matrix(seq(1,24,1), nrow=6, ncol = 4)
yan2 <- matrix(seq(25,50,1), nrow=6, ncol = 4)
  1. factor() – converts vector into factor, finds unique characters/strings & sorts alphabetically
STEM<- c("science","math", "technology", "engineering","math")
STEM_factor <- factor(STEM)
print(STEM_factor)
## [1] science     math        technology  engineering math       
## Levels: engineering math science technology
  1. rbind() – combines two separate data matrices by row, has to be of same dataset or else combination meaningless
rbind(yan,yan2)
##       [,1] [,2] [,3] [,4]
##  [1,]    1    7   13   19
##  [2,]    2    8   14   20
##  [3,]    3    9   15   21
##  [4,]    4   10   16   22
##  [5,]    5   11   17   23
##  [6,]    6   12   18   24
##  [7,]   25   31   37   43
##  [8,]   26   32   38   44
##  [9,]   27   33   39   45
## [10,]   28   34   40   46
## [11,]   29   35   41   47
## [12,]   30   36   42   48
  1. cbind() – combines two separate data matrices by column, has to be of same dataset or else combination meaningless
cbind(yan, yan2)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    7   13   19   25   31   37   43
## [2,]    2    8   14   20   26   32   38   44
## [3,]    3    9   15   21   27   33   39   45
## [4,]    4   10   16   22   28   34   40   46
## [5,]    5   11   17   23   29   35   41   47
## [6,]    6   12   18   24   30   36   42   48

Slicing and Extracting Data

Indexing vectors

  1. x[n] nth element – indexes the number or character by its position within the vector or matrix as specified by number in bracket
g <- matrix(1:20, nrow = 2, ncol = 10)
g[2]
## [1] 2
h <- c(4,8,21,34,51,83)
h [4]
## [1] 34
  1. x[-n] all but nth element – indexes entire vector or matrix except the specified element
g[-7]
##  [1]  1  2  3  4  5  6  8  9 10 11 12 13 14 15 16 17 18 19 20
  1. x[1:n] first n elements – indexes the sequence of numbers specified within the brackets from a vector or matrix
g[2:4]
## [1] 2 3 4
h[1:3]
## [1]  4  8 21
  1. x[-(1:n)] elements from n+1 to the end – indexes all elements of a vector or matrix except those specified within the parentheses.
h[-(2:4)]
## [1]  4 51 83
g[-(5:10)]
##  [1]  1  2  3  4 11 12 13 14 15 16 17 18 19 20
  1. x[c(1,4,2)] specific elements – indexes specific (combinations of) numbers from a vector or matrix
h[c(1,5)]
## [1]  4 51
g[c(2,6,20)]
## [1]  2  6 20
  1. x[“name”] elements named “name”
j <- c("john", "emily", "ana", "frank", "cam")
j["ana"]
## [1] NA
class(j)
## [1] "character"
beb<- table(j)
b<- rep(beb,5)  # ???? not replicating?!
names(b)
##  [1] "ana"   "cam"   "emily" "frank" "john"  "ana"   "cam"   "emily"
##  [9] "frank" "john"  "ana"   "cam"   "emily" "frank" "john"  "ana"  
## [17] "cam"   "emily" "frank" "john"  "ana"   "cam"   "emily" "frank"
## [25] "john"
b["john"]
## john 
##    1
# wont work as a vector. make it a table first
#or names function
  1. x[x>3] all elements greater than 3 – indexes numbers either greater than or less than the specified number
g[g>12]
## [1] 13 14 15 16 17 18 19 20
h[h<50]
## [1]  4  8 21 34
  1. x[x > 3 & x < 5] all elements between 3 and 5 – indexes numbers with logical statements
g[g > 6 & g< 12]
## [1]  7  8  9 10 11
h[h > 20 & h < 40]
## [1] 21 34
  1. x[x %in% c(“a”,“and”,“the”)] all elements in given set
"ana" %in% j
## [1] TRUE
"john" %in% b == FALSE
## [1] TRUE

Indexing lists

song<- list("Hotel California", 378, 4)

names(song) <- c("title", "duration", "track")

similar_song <- list (title = "Misery Business", duration= 190, track = 5)

song<- list (title = "Hotel California", duration = 378, track = 4, similar = similar_song)

str(song)
## List of 4
##  $ title   : chr "Hotel California"
##  $ duration: num 378
##  $ track   : num 4
##  $ similar :List of 3
##   ..$ title   : chr "Misery Business"
##   ..$ duration: num 190
##   ..$ track   : num 5
#https://www.youtube.com/watch?v=Px9VNWHja4M
  1. x[n] list with elements n – indexes main list’s nth item
song[2]
## $duration
## [1] 378
  1. x[[n]] nt h element of the list
song[[4]][1]
## $title
## [1] "Misery Business"
#2nd song
  1. x[[“name”]] element of the list named “name”
song[["similar"]]["duration"]
## $duration
## [1] 190
  1. x$name id.
song$similar$duration
## [1] 190
#how do i get to the 2nd "song" 

Indexing matrices

as<- c(31,123,67,1123,987)
ss<- c(122,56,897,32,45)
sb<- list(as,ss)
sbb<-matrix(data=as & ss, nrow=5, ncol = 2, byrow=FALSE, dimnames = NULL)

#why doesnt a list work in a matrix if they're both numeric
  1. x[i,j] element at row i, column j
sbb[1,2]
## [1] TRUE
  1. x[i,] row i
sbb[4]
## [1] TRUE
  1. x[,j] column j
sbb[,2]
## [1] TRUE TRUE TRUE TRUE TRUE
  1. x[,c(1,3)] columns 1 and 3
sbb[c(2,3)]
## [1] TRUE TRUE
  1. x[“name”,] row named “name”
first<-matrix(1:9, nrow=3)
rownames(first)<-(c("a","b","c"))

first["c",]
## [1] 3 6 9

Indexing data frames (matrix indexing plus the following)

name<- c("ana","pete", "dave")
age<- c(22,32,45)
pass<-c(TRUE,FALSE,FALSE)

nap<-data.frame(name,age,pass)
nap
##   name age  pass
## 1  ana  22  TRUE
## 2 pete  32 FALSE
## 3 dave  45 FALSE
  1. x[[“name”]] column named “name” – indexes variables within specified column
nap[["age"]]
## [1] 22 32 45
  1. x$nameid. – indexes variables within specified column
nap$pass
## [1]  TRUE FALSE FALSE

Variable Conversion

  1. as.data.frame(x) – converts r objects into data frame format
as.data.frame(name)
##   name
## 1  ana
## 2 pete
## 3 dave
  1. as.numeric(x) – change the composition of a vector into numbers (if possible)
dod <- c(5,"4",7)
as.numeric(dod)
## [1] 5 4 7
  1. as.logical(x) – converts all non-zero numbers to True and all zeros as false
ll<- c(0,4,7,2,0,1)
as.logical(ll)
## [1] FALSE  TRUE  TRUE  TRUE FALSE  TRUE
  1. as.character(x) – converts numbers into characters
cob<- 5.692
cob<- as.character(cob)

Variable Information

  1. is.na(x) – determines if any values are missing or not available (??)
na <- c(4,7,NA,21)
is.na(na)
## [1] FALSE FALSE  TRUE FALSE
  1. is.null(x) – checks if there is a null variable
is.null(age)
## [1] FALSE
  1. is.data.frame(x) – checks if object is in a data frame format
is.data.frame(nap)
## [1] TRUE
  1. is.numeric(x) – checks if variable/object is a number (returns logical)
is.numeric(cob)
## [1] FALSE
  1. is.character(x) – checks if variable/item is a character class
is.character(cob)
## [1] TRUE
  1. length(x) – returns length of vector
length(a)
## [1] 4
  1. dim(x) – returns dimension of object(vector/list/matrix etc)
dim(nap)
## [1] 3 3
  1. dimnames(x) – lists dimensions/columns and their names
dimnames(nap)
## [[1]]
## [1] "1" "2" "3"
## 
## [[2]]
## [1] "name" "age"  "pass"
  1. nrow(x) – returns how many rows are in a matrix/dataframe & sets number of rows
nrow(nap)
## [1] 3
  1. ncol(x) – returns number of columns in a matrix/dataframe & sets number of columns
ncol(nap)
## [1] 3
  1. class() – states a variable’s class i.e. char, num, integer
class(ans)
## [1] "numeric"
  1. attributes() – displays attributs of object
attributes(yan)
## $dim
## [1] 6 4

Data Selection and Manipulation

  1. which.max() – returns position of the highest value
which.max(a)
## [1] 4
  1. which.min() – returns position of the lowest value
which.min(a)
## [1] 2
  1. which() – returns position of which numbers fit whatever criteria you provide
v <- c(6,3,7,1,8)
which(v %% 2 ==0)
## [1] 1 5
  1. sort() – sorts a vector from lowest to highest
sort(v)
## [1] 1 3 6 7 8
  1. unique() – returns the unique values within a vector/list/matrix
w <- c(1,1,3,6,6,3,4,4)
unique(w)
## [1] 1 3 6 4
  1. table() – creates a table that keeps a count of the frequency of unique characters
vef<-c("sd","asf","ag","sd","rg","asf")
table(vef)
## vef
##  ag asf  rg  sd 
##   1   2   1   2
  1. sample() – takes a sample of numbers, random numbers under specified restrictions.
sample(100, 10, replace=FALSE)
##  [1] 65 32 40 83 55 50 16 43 58 86

Math

  1. max() – returns the maximum number of a set or vector
felix <- seq(3,56,1.2)
max(felix)
## [1] 55.8
  1. min() – returns the minimum number of a set or vector
min(felix)
## [1] 3
  1. range() – returns the minimum and maximum of a set or vector
range(felix)
## [1] 52.8
  1. sum() – calculates the sum of a number set or vector
s_u <-sum(felix)
s_u
## [1] 1323
  1. mean() – function to find the mean of a number set or vector
m_ <-mean(c(3,7,12,99,23,54))
m_
## [1] 33
  1. median() – finds the median of a number set or vector
nad<- seq(1,50,3)
z<- median(nad)
z
## [1] 25
  1. var() – computes variance
ka <- c(1,5,3)
an <- c(4,5,8)

var(ka)
## [1] 4
  1. sd() – computes standard deviation
sd(an)
## [1] 2.081666
  1. cor() – computes correlation
cor(ka,an)
## [1] 0.2401922
  1. round() – rounds the value of input
round(9/2)
## [1] 4
  1. abs() – returns absolute value
abs(-5)
## [1] 5

Matrices

gre<-matrix(2:20, nrow=5)
  1. t() – transposes matrix
t(first)
##      a b c
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
  1. diag() – returns numbers within the matrix that are in diagonal positions
diag(first)
## [1] 1 5 9
  1. rowSums() – returns sums of the rows
rowSums(gre)
## [1] 38 42 46 50 35
  1. colSums() – returns sums of the columns
colSums(gre)
## [1] 20 45 70 76
  1. rowMeans() – returns means of the rows
rowMeans(gre)
## [1]  9.50 10.50 11.50 12.50  8.75
  1. colMeans() – returns means of the columns
colMeans(gre)
## [1]  4.0  9.0 14.0 15.2

Advanced Data processing

  1. apply() – applies specified functions on matrices. 2nd number is margin 1=rows, 2=columns
gre
##      [,1] [,2] [,3] [,4]
## [1,]    2    7   12   17
## [2,]    3    8   13   18
## [3,]    4    9   14   19
## [4,]    5   10   15   20
## [5,]    6   11   16    2
apply(gre,2,sum)
## [1] 20 45 70 76
  1. aggregate() –?
aggregate(gre)
#dont understand the function

Strings

  1. paste() – similar to concatenate, has separators
rat1 <- "17R1"
paste(rat1, "box", sep = " is in ")
## [1] "17R1 is in box"
  1. strsplit() –

  2. tolower() –

alpha<- LETTERS
tolower(alpha)
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
  1. toupper –
alphab<- letters
toupper(alphab)
##  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
## [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"

Plotting

  1. hist() - function for creating a histogram
uk <- rnorm(50,mean=20,sd=1)
hist(uk)

  1. plot() – function for plotting a graph
plot(rnorm(100,mean=38,sd=2))

Distributions

  1. rnorm()
q<- rnorm(20, mean = 5, sd = 2)
plot(q)

  1. runif() – generates random numbers (how many, min, max)
runif(10,5,50)
##  [1] 44.092221 25.278331 21.340110 14.973972 40.459667 10.419702 35.577748
##  [8]  7.832178 13.582619  7.840849

Programming

  1. Show that you can define a function
mult4<- function(m)
  return(m*4)

mult4(10)
## [1] 40
  1. Show that you can write a for loop
d<- c(81,21,52,36,19,22)
for(s in 1:4)
  d[s]<-d[s]+1
print(d)
## [1] 82 22 53 37 19 22
  1. Show that you can write a while loop
dud<- c("hello","R")
ded<- 4:12
while(ded>10){
    print(dud)
    ded=ded+1
}
  1. Show that you can write an if else statement
a <- c(6,1,4,9)
ifelse(a%%2 == 0, "even","odd")
## [1] "even" "odd"  "even" "odd"
  1. Explain what return() does inside a function, show you can use it Return is used within functions to assign a number, value, r object to be brought back after the function completes.
add2<-function(x){
    return(x+2)
}
add2(5)
## [1] 7
  1. Explain what break() does, show you can use it Break is a function that controls the flow of a loop, it stops the iterations and flows out. Inside nested loops the flow exists the innermost loop.
f<- c(3,1,5,32,11,42)
f_num<-1:length(f)
for(g in f_num){
  if(f[g]<30){
    f[g]<- f[g]+3
}
  if(f[g]>30){
    break
    
  }
  }

print(f[g])
## [1] 32