Rcmds From Class
Rcmds From Class
borade.vijay@gmail.com
Table of Useful R commands
Command Purpose
help() Obtain documentation for a given R command
example() View some examples on the use of a command
Command Purpose
c(), scan() Enter data manually to a vector in R
Sharing or publishing the contents in part or full is liable for legal action.
Examples of usage
help()
help(mean)
example()
require(lattice)
example(histogram)
[1] 8 6 7 5 3 0 9
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
borade.vijay@gmail.com
KP2WFEJ3RX[49] 0 0 0 0
[1] 7.0 8.5 10.0 11.5 13.0 14.5 16.0 17.5 19.0 20.5 22.0 23.5 25.0 26.5 28.0 29.5 31.0 32.5 34.0
[20] 35.5 37.0 38.5 40.0
> dim(iris)
[1] 150 5
> str(iris)
> View(iris)
2
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
ls(), rm()
> data(iris)
> data(faithful)
> data(Puromycin)
> data(LakeHuron)
> ls()
> rm(faithful)
> ls()
hist()
data(faithful)
hist(faithful$eruptions)
hist(faithful$eruptions, n=15)
hist(faithful$eruptions, breaks=seq(1.5,5.25,.25), col="red")
borade.vijay@gmail.com
KP2WFEJ3RXhist(faithful$eruptions, freq=F, n=15, main="Histogram of Old Faithful Eruption Times", xlab="Duration (mins)")
0.4
0.2
0.0
Duration (mins)
3
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
library(), require()
> library(abd)
> require(lattice)
histogram()
require(lattice)
data(iris)
histogram(iris$Sepal.Length, breaks=seq(4,8,.25))
histogram(~ Sepal.Length, data=iris, main="Iris Sepals", xlab="Length")
histogram(~ Sepal.Length | Species, data=iris, col="red")
histogram(~ Sepal.Length | Species, data=iris, n=15, layout=c(1,3))
read.csv()
read.table()
borade.vijay@gmail.com
> counties=read.csv("http://www.calvin.edu/~stob/data/counties.csv")
KP2WFEJ3RX> names(counties)
> x = counties$LandArea
> mean(x, na.rm = T)
[1] 1126.214
[1] 616.48
> summary(x)
[1] 3622.453
[1] 13122165
4
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
sum()
> firstTwentyIntegers = 1:20
> sum(firstTwentyIntegers)
[1] 210
[1] 0.14
stem()
> monarchs = read.csv("http://www.calvin.edu/~scofield/data/comma/monarchReigns.csv")
> stem(monarchs$years)
0 | 0123566799
1 | 0023333579
2 | 012224455
3 | 355589
4 | 4
5 | 069
6 | 3
borade.vijay@gmail.com
table(), table(), mosaicplot(), cut()
KP2WFEJ3RX
> pol = read.csv("http://www.calvin.edu/~stob/data/csbv.csv")
> table(pol$sex)
Female Male
133 88
sex
Female Male
133 88
Political07
Political04 Conservative Far Left Far Right Liberal Middle-of-the-road
Conservative 58 0 2 13 39
Far Right 4 0 3 0 0
Liberal 0 1 1 14 4
Middle-of-the-road 20 0 0 22 32
5
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
pol
Female
sex
Male
Political04
0 1 2 3 5 6 7 9 10 12 13 15 17 19 20 21 22 24 25 33 35 38 39 44 50 56 59 63
1 1 1 1 1 2 1 2 2 1 4 1 1 1 1 1 3 2 2 1 3 1 1 1 1 1 1 1
years
0 1 2 3 5 6 7 9 10 12 13 15 17 19 20 21 22 24 25 33 35 38 39 44 50 56 59 63
1 1 1 1 1 2 1 2 2 1 4 1 1 1 1 1 3 2 2 1 3 1 1 1 1 1 1 1
(0,5] (5,10] (10,15] (15,20] (20,25] (25,30] (30,35] (35,40] (40,45] (45,50] (50,55] (55,60]
4 7 6 3 8 0 4 2 1 1 0 2
(60,65]
1
fiveYrLevels
(0,5] (5,10] (10,15] (15,20] (20,25] (25,30] (30,35] (35,40] (40,45] (45,50] (50,55] (55,60]
4 7 6 3 8 0 4 2 1 1 0 2
(60,65]
1
6
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
barplot()
pol = read.csv("http://www.calvin.edu/~stob/data/csbv.csv")
barplot(table(pol$Political04), main="Political Leanings, Calvin Freshman 2004")
barplot(table(pol$Political04), horiz=T)
barplot(table(pol$Political04),col=c("red","green","blue","orange"))
barplot(table(pol$Political04),col=c("red","green","blue","orange"),
names=c("Conservative","Far Right","Liberal","Centrist"))
80
40
0
borade.vijay@gmail.com
barplot(xtabs(~sex + Political04, data=pol), legend=c("Female","Male"), beside=T)
KP2WFEJ3RX
60
Female
Male
50
40
30
20
10
0
7
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
boxplot()
data(iris)
boxplot(iris$Sepal.Length)
boxplot(iris$Sepal.Length, col="yellow")
boxplot(Sepal.Length ~ Species, data=iris)
boxplot(Sepal.Length ~ Species, data=iris, col="yellow", ylab="Sepal length",main="Iris Sepal Length by Species")
6.5
5.5
●
4.5
plot()
borade.vijay@gmail.com
KP2WFEJ3RX
data(faithful)
plot(waiting~eruptions,data=faithful)
plot(waiting~eruptions,data=faithful,cex=.5)
plot(waiting~eruptions,data=faithful,pch=6)
plot(waiting~eruptions,data=faithful,pch=19)
plot(waiting~eruptions,data=faithful,cex=.5,pch=19,col="blue")
plot(waiting~eruptions, data=faithful, cex=.5, pch=19, col="blue", main="Old Faithful Eruptions",
ylab="Wait time between eruptions", xlab="Duration of eruption")
●
●
90
● ● ●
● ● ●● ● ● ● ●
● ● ●● ● ● ● ●
● ● ●● ● ● ●
● ● ● ●●
●●●● ●●
● ● ● ● ● ● ●● ● ●● ●●●●●● ● ● ● ●
● ●
● ● ●● ● ● ● ● ●● ●● ●●
● ● ●
● ● ● ●● ● ●● ●● ●● ●●● ● ●● ● ●●●●●●●●
● ●●●
●
● ●●● ● ●● ●●
● ● ● ● ● ●
● ●●
● ● ●● ●●● ●● ●
●
● ● ● ● ●●●
70
● ● ●
● ● ● ●
● ●● ●●
● ● ●
● ● ●
● ● ● ● ● ● ●
● ● ● ● ● ● ●
●● ● ●● ●●● ● ●●
●● ●
●●● ●● ● ● ● ●
●●●●●
●● ● ● ●●● ● ●● ● ●●
50
● ●● ●● ●● ●● ● ● ●
●
● ●● ● ● ● ● ● ●
● ●
● ● ●
●●●● ● ●●
●
Duration of eruption
8
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
sample()
> sample(c("Heads","Tails"), size=1)
[1] "Heads"
[1] "Heads" "Heads" "Heads" "Tails" "Tails" "Tails" "Tails" "Tails" "Tails" "Heads"
[1] 1 0 0 1 1 0 0 1 0 0
[1] 10
[1] 1 1 1 0 1 1 1 1 1 1
[1] 0 0 0 0 0
replicate()
> sample(c("Heads","Tails"), 2, replace=T)
0.15
0.00
0 2 4 6 8 10
9
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
[1] 0.03125
[1] 0.5
[1] 0.5
flip5
0 1 2 3 4 5
0.0310 0.1545 0.3117 0.3166 0.1566 0.0296
0 1 2 3 4 5
0.0304 0.1587 0.3087 0.3075 0.1634 0.0313
> prop.test(29, 200, .21) # inference on same sample, using normal approx. to binomial
10
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
[1] 0.679813
[1] 0.679813
data: observedCounts
X-squared = 3.1309, df = 5, p-value = 0.6798
borade.vijay@gmail.com
KP2WFEJ3RX
addmargins()
> blood = read.csv("http://www.calvin.edu/~scofield/data/comma/blood.csv")
> t = table(blood$Rh, blood$type)
> addmargins(t) # to add both row/column totals
A AB B O Sum
Neg 6 1 2 7 16
Pos 34 3 9 38 84
Sum 40 4 11 45 100
A AB B O
Neg 6 1 2 7
Pos 34 3 9 38
Sum 40 4 11 45
A AB B O Sum
Neg 6 1 2 7 16
Pos 34 3 9 38 84
11
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
prop.table()
> summary(smoke)
> prop.table(smoke)
> prop.table(smoke, 1)
borade.vijay@gmail.com
KP2WFEJ3RX High Low Middle
current 0.4396552 0.3706897 0.1896552
former 0.6524823 0.1985816 0.1489362
never 0.6868687 0.2222222 0.0909091
current
former
never
0
12
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
par()
> par(mfrow = c(1,2)) # set figure so next two plots appear side-by-side
> poisSamp = rpois(50, 3) # Draw sample of size 50 from Pois(3)
> maxX = max(poisSamp) # will help in setting horizontal plotting region
> hist(poisSamp, freq=F, breaks=-.5:(maxX+.5), col="green", xlab="Sampled values")
> plot(0:maxX, dpois(0:maxX, 3), type="h", ylim=c(0,.25), col="blue", main="Probabilities for Pois(3)")
dpois(0:maxX, 3)
0.20
Density
0.15
0.10
0.00
0.00
0 2 4 6 0 1 2 3 4 5 6 7
fisher.test()
> blood = read.csv("http://www.calvin.edu/~scofield/data/comma/blood.csv")
> tblood = xtabs(~Rh + type, data=blood)
> tblood # contingency table for blood type and Rh factor
type
Rh A AB B O
Neg 6 1 2 7
Pos 34 3 9 38
> chisq.test(tblood)
data: tblood
X-squared = 0.3164, df = 3, p-value = 0.957
> fisher.test(tblood)
data: tblood
p-value = 0.8702
alternative hypothesis: two.sided
13
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
dpois(), ppois()
> dpois(2:7, 4.2) # probabilities of 2, 3, 4, 5, 6 or 7 successes in Pois(4.211)
> ppois(1, 4.2) # probability of 1 or fewer successes in Pois(4.2); same as sum(dpois(0:1, 4.2))
[1] 0.077977
[1] 0.06394334
[1] 0.2524925
> qnorm(c(.95, .975, .995)) # obtain z* critical values for 90, 95, 99% CIs
> nSamp = rnorm(10000, 7, 1.5) # draw random sample from Norm(7, 1.5)
> hist(nSamp, freq=F, col="green", main="Sampled values and population density curve")
> xs = seq(2, 12, .05)
> lines(xs, dnorm(xs, 7, 1.5), lwd=2, col="blue")
borade.vijay@gmail.com
KP2WFEJ3RX Sampled values and population density curve
0.20
Density
0.10
0.00
2 4 6 8 10 12
nSamp
14
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
> qt(c(.95, .975, .995), df=9) # critical values for 90, 95, 99% CIs for means
[1] 0.02980016
> tSamp = rt(50, 11) # takes random sample of size 50 from t-dist with 11 dfs
> # code for comparing several t distributions to standard normal distribution
> xs = seq(-5,5,.01)
> plot(xs, dnorm(xs), type="l", lwd=2, col="black", ylab="pdf values",
+ main="Some t dists alongside standard normal curve")
> lines(xs, dt(xs, 1), lwd=2, col="blue")
> lines(xs, dt(xs, 4), lwd=2, col="red")
> lines(xs, dt(xs, 10), lwd=2, col="green")
> legend("topright",col=c("black","blue","red","green"),
+ legend=c("std. normal","t, df=1","t, df=4","t, df=10"), lty=1)
borade.vijay@gmail.com
std. normal
pdf values
KP2WFEJ3RX t, df=1
t, df=4
0.2
t, df=10
0.0
−4 −2 0 2 4
xs
by()
> data(warpbreaks)
> by(warpbreaks$breaks, warpbreaks$tension, mean)
warpbreaks$tension: L
[1] 36.38889
---------------------------------------------------------------------------
warpbreaks$tension: M
[1] 26.38889
---------------------------------------------------------------------------
warpbreaks$tension: H
[1] 21.66667
15
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
t.test()
> data(sleep)
> t.test(extra ~ group, data=sleep) # 2-sample t with group id column
qqnorm(), qqline()
● ●
●
●●
50
●●●●●
●●●●
●●●●●
●●●●●
●●●●●●●●
●●●●●●●
●●●●
30
●●●●
●●●●●
●●
●
●
●●●
●●●●
10
● ●
● ● ● ●
−2 −1 0 1 2
Theoretical Quantiles
16
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.
R Commands for MATH 143 Examples of usage
power.t.test()
> power.t.test(n=20, delta=.1, sd=.4, sig.level=.05) # tells how much power at these settings
n = 20
delta = 0.1
sd = 0.4
sig.level = 0.05
power = 0.1171781
alternative = two.sided
> power.t.test(delta=.1, sd=.4, sig.level=.05, power=.8) # tells sample size needed for desired power
n = 252.1281
delta = 0.1
sd = 0.4
sig.level = 0.05
power = 0.8
alternative = two.sided
Response: shift
Df Sum Sq Mean Sq F value Pr(>F)
treatment 2 7.2245 3.6122 7.2894 0.004472 **
Residuals 19 9.4153 0.4955
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
●
●
● ●
●
0 ●
●
●
● ●
shift
●
●
●
●
−1 ●
●
●
●
● ●
−2 ●
treatment
17
This file is meant for personal use by borade.vijay@gmail.com only.
Sharing or publishing the contents in part or full is liable for legal action.