Notes on R
R Notes
Common R Stuff
Download and Install R Package
Load a Module
Scrape an HTML Table
R Tutorial
Basic R
R help
getting help with ?<command-name>
- type a ?rnorm, to pop open a manual Page on the R command rnorm
- or try ?boxplot to get a help page on the R boxplot function
Using Famous Datasets
Reading Data into R from Files
Reading Data from STDIN
- To read data from STDIN, call the scan function with the file parameter left blank
- Enter a blank line or Ctrl D to end data input
Reading a Line of Space Separated Data into a vector
```r
nums <- scan(textConnection("75 48 61 48 150 49 57 39 27 51 46 50 62 51 50 58 38 34 59 44 24 39 40 33 49 33 34 32 35 30 23 39 36 25 20 32 43 52 42 44 46 51 47 51 44 33 38"), sep=" ")
median(nums)
mean(nums)
deaths <- nums[-5]
mean(deaths)
median(deaths)
sd(deaths)
```
```r
: [1] 44
: [1] 44.93617
: [1] 42.65217
: [1] 43.5
: [1] 11.48761
```
Generating a Histogram
```R
# Data pasted from another document can be placed in a vector
# via the following composition of functions
# textConnection can also be used to read data from stdin
nums <- scan(textConnection("75 48 61 48 150 49 57 39 27 51 46 50 62 51 50 58 38 34 59 44 24 39 40 33 49 33 34 32 35 30 23 39 36 25 20 32 43 52 42 44 46 51 47 51 44 33 38"), sep=" ")
hist(nums, main="US Lightning Death's 1959-2005")
```
Trimmed Mean to the Rescue
Drawing a Scatterplot with a Linear Regression line
Putting 2 plots on 1 image
Using Reduce and Map
Reduce(f=function,x=vector)
Reduce takes a vector of values, and a binary function and accumulates the values returned over the entire vector of values.
Map(f=function(x){..},x=vector)
Map takes a vector of values and a unary function, runs the function on each value and returns the vector of return values.
here's how to combine them
This function returns the cumulative distribution function of P(x<4) of X~poisson(5).