From Learn X in Y Minutes http://learnxinyminutes.com/docs/r/
# comments are like this, no multi-line
ls()
## character(0)
# this is to browse pre-loaded datasets
data()
#Notice what ls() shows now.
# load in "Lengths of Major North American rivers"
data(rivers)
ls()
## [1] "rivers"
# head of the dataset
head(rivers)
## [1] 735 320 325 392 524 450
head(rivers, 10)
## [1] 735 320 325 392 524 450 1459 135 465 600
# how many rivers in this set?
length(rivers)
## [1] 141
# some summary stats
summary(rivers)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 135.0 310.0 425.0 591.2 680.0 3710.0
# stem and leaf plot
stem(rivers)
##
## The decimal point is 2 digit(s) to the right of the |
##
## 0 | 4
## 2 | 011223334555566667778888899900001111223333344455555666688888999
## 4 | 111222333445566779001233344567
## 6 | 000112233578012234468
## 8 | 045790018
## 10 | 04507
## 12 | 1471
## 14 | 56
## 16 | 7
## 18 | 9
## 20 |
## 22 | 25
## 24 | 3
## 26 |
## 28 |
## 30 |
## 32 |
## 34 |
## 36 | 1
# some histograms
hist(rivers, col='#999999', border='white', breaks=25, main='North American Rivers')
hist(rivers, col='#999999', border='white', breaks=50)
hist(rivers, col='#999999', border='black', breaks=100)
hist(log(rivers), col="#333333", border="white", breaks=25)
data(discoveries)
plot(discoveries, col="#999999", lwd=3, xlab="Year",
main="Number of important discoveries per year")
plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year",
main="Number of important discoveries per year")
# sort the data
discoveries
## Time Series:
## Start = 1860
## End = 1959
## Frequency = 1
## [1] 5 3 0 2 0 3 2 3 6 1 2 1 2 1 3 3 3 5 2 4 4 0 2
## [24] 3 7 12 3 10 9 2 3 7 7 2 3 3 6 2 4 3 5 2 2 4 0 4
## [47] 2 5 2 3 3 6 5 8 3 6 6 0 5 2 2 2 6 3 4 4 2 2 4
## [70] 7 5 3 3 0 2 2 2 1 3 4 2 2 1 1 1 2 1 4 4 3 2 1
## [93] 4 1 1 1 0 0 2 0
sort(discoveries)
## [1] 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 2
## [24] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [47] 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4
## [70] 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6
## [93] 7 7 7 7 8 9 10 12
stem(discoveries, scale=2)
##
## The decimal point is at the |
##
## 0 | 000000000
## 1 | 000000000000
## 2 | 00000000000000000000000000
## 3 | 00000000000000000000
## 4 | 000000000000
## 5 | 0000000
## 6 | 000000
## 7 | 0000
## 8 | 0
## 9 | 0
## 10 | 0
## 11 |
## 12 | 0
stem(discoveries, scale=1)
##
## The decimal point is at the |
##
## 0 | 000000000000000000000
## 2 | 0000000000000000000000000000000000000000000000
## 4 | 0000000000000000000
## 6 | 0000000000
## 8 | 00
## 10 | 0
## 12 | 0
max(discoveries)
## [1] 12
# rolling a die seven times
round(runif(7, min=.5, max=6.5))
## [1] 2 4 1 2 5 3 3
# draw gaussian 9 times
rnorm(9)
## [1] -1.94710520 -0.26956978 -1.84880537 0.03681048 0.24915903 -0.55617432
## [7] 0.52402668 -1.08124181 -0.61907896