From 678c4fdfa276195d74a7ee1b51c1334563e6548e Mon Sep 17 00:00:00 2001 From: Pietra Ferreira Date: Tue, 12 Sep 2023 21:36:16 +0100 Subject: [PATCH] vault backup: 2023-09-12 21:36:16 Affected files: content/notes/university/year3/cs3002/cs3002-lab1.md --- .../university/year3/cs3002/cs3002-lab1.md | 66 +++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/content/notes/university/year3/cs3002/cs3002-lab1.md b/content/notes/university/year3/cs3002/cs3002-lab1.md index de38e8500fe3..b23861c08822 100644 --- a/content/notes/university/year3/cs3002/cs3002-lab1.md +++ b/content/notes/university/year3/cs3002/cs3002-lab1.md @@ -11,7 +11,8 @@ year: '3' --- The lab sheet can be found [here](assets/university/year3/R_IntroLab_v3.pdf). -## Notes +# Notes +--- - R is **case-sensitive**, so `view` doesn't work but `View` does. - To create scatterplots: - Column name: @@ -35,14 +36,71 @@ The lab sheet can be found [here](assets/university/year3/R_IntroLab_v3.pdf). lmfire=line(mydata$ISI~mydata$temp) abline(coef(lmfire)) ``` - -## R Cheatsheets +## A (very) short introduction to R +--- +R is for statistical computing and graphics, and iti s a public domain (GNU) project. + +To set the working directory: `setwd("/Users/pietra/etc")`. + +To check the installed packages: `library()` in the console. +- To install: `install.packages("geometry")`. +- We then load: `library("geometry")`. + +To remove all variables from memory: `rm(list=ls())`. + +To define a vector we use **c** meaning **concatenate**: `b=c(3,4,5)`. + +- `rnorm()`: creates random samples from a normal distribution. + - `rnorm(10, mean=1.2, sd=3.4)`. + +We can then do something like: + +```r +x = rnorm(100) +plot(x) +``` + +In doubt, we can do `help(rnorm)` or even `example(rnorm)`. + +- `CTRL+SHIFT+S` runs the whole script, whereas `CTRL+ENTER` runs a line. + +### Vectors +**WE INDEX FROM 1, NOT 0!!!** + +- To construct: `vec1 = c(1,4,5,8,10)`. + - Alternatively: `vec1 = seq(from=0, to=1, by=0.25)`. +- To change a value: `vec1[3] = 12`. +- We can then do things like: `sum(vec1)`. + +### Matrices +2-d vectors. + +- To construct: `mat=matrix(data=c(9,2,3,4,5,6),ncol=3)`. + - Can also specify `nrow`. + - We get: + +| | \[,1\] | \[,2\] | \[,3\] | +| ------ | ------ | ------ | ------ | +| \[1,\] | 9 | 3 | 5 | +| \[2,\] | 2 | 4 | 6 | + +Some operations are: +- Address an element: `mat[1,2]`. +- Select a whole row: `mat[2,]`. +- Function example: `mean(mat)`. + +# R Cheatsheets - [Graph Parameters CheatSheet](assets/university/year3/R_Graph_Parameters_CheatSheet.pdf). - [Base R CheatSheet](assets/university/year3/Base_R_CheatSheet.pdf). - [Data Transformation CheatSheet](assets/university/year3/R_Data_Transformation_CheatSheet.pdf). -## Commands +- Full manual [here](http://cran.r-project.org/doc/manuals/%20R-intro.pdf). +- Short [reference card](http://zoonek2.free.fr/UNIX/48_R/all.html). +- Examples [here](http://rwiki.sciviews.org/doku.php). +- Typical [user wiki](http://www.statmethods.net/). + +# Commands ```r mydata = read.csv('/Users/pietra/projects/university/year3/cs3002/lab1/forestfires.csv', sep=',')