Merge remote-tracking (subtree) branch 'PracticingR/master'

This commit is contained in:
Dan Buch 2012-03-03 20:22:22 -05:00
commit c71dccccc4
9 changed files with 545 additions and 0 deletions

6
PracticingR/README.tex Normal file
View File

@ -0,0 +1,6 @@
\begin{document}
\section{Practicing R}
Where I practice and learn and destroy me some R.
\end{document}

6
PracticingR/README.txt Normal file
View File

@ -0,0 +1,6 @@
Practicing R
Where I practice and learn and destroy me some R.

Binary file not shown.

View File

@ -0,0 +1,367 @@
library(MASS)
painters
painters$School
school = painters$School
school.freq = table(school)
school.freq
cbind(school.freq)
head(painters)
composition = painters$Composition
composition.freq = table(composition)
cbind(composition.freq)
school
school.freq
school.freq[1]
help(max)
school[,][1]
school[1,][1]
school[1][1]
school[1]
school.freq[1,]
school.freq[1]
school.freq[1][1]
school.freq[1]
help(typeof)
typeof(school.freq[1])
max(school.freq)
max(school)
max(school.freq)
school.relfreq = school.freq / nrow(painters)
school.relfreq
settings = options(digits=1)
school.relfreq
options(settings)
cbind(school.relfreq)
composition.freq
composition.relfreq = composition.freq / nrow(composition)
composition.relfreq = composition.freq / nrow(painters)
composition.relfreq
cbind(composition.relfreq)
# bar graph exercise
library(MASS)
school = painters$School
school.freq = table(school)
barplot(school.freq)
colors = c("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan")
barplot(schools.freq, col=colors)
barplot(school.freq, col=colors)
composition = painters$Composition
composition.freq = table(composition)
count.fields(composition.freq)
ncol(composition.freq)
composition.freq
nrow(composition.freq)
nrow(school.freq)
colors
barplot(composition.freq, col=colors)
q()
library(MASS)
school = painters$school
school
school = painters$School
school
school.freq = table(school)
pip(school.freq)
pie(school.freq)
colors = c("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan")
pie(school.freq, col=colors)
composition = painters$Composition
composition.freq = table(composition)
pip(composition.freq, col=colors)
pie(composition.freq, col=colors)
library(MASS)
school = painters$School
c_school = school == "C"
c_painters = painters[c_school,]
mean(c_painters$Composition)
pie(tapply(painters$Composition, painters$School, mean)
pie(tapply(painters$Composition, painters$School, mean))
barchart(tapply(painters$Composition, painters$School, mean))
barplot(tapply(painters$Composition, painters$School, mean))
q()
head(faithful)
duration = faithful$eruptions
range(duration)
breaks = seq(1.5, 5.5, by=0.5)
breaks
duration.cut = cut(duration, breaks, right=FALSE)
duration.freq = table(duration.cut)
duration.freq
cbind(duration.freq)
barplot(duration.freq)
nrow(faithful)
faithful$eruptions / nrow(faithful)
help(hist)
hist(faithful$eruptions)
faithful)
head(faithful)
hist(faithful$waiting)
hist(faithful)
help(barplot)
VADeaths
help(demo)
demo()
demo("graphics")
hist(faithful$waiting)
interval = faithful$waiting
interval
range(interval)
breaks = seq(43, 96, by=1)
breaks
interval.cut = cut(interval, breaks, right=FALSE)
interval.freq = table(interval.cut)
barplot(interval.freq)
breaks = seq(43, 96, by=5)
breaks
interval.cut = cut(interval, breaks, right=FALSE)
interval.freq = table(interval.cut)
barplot(interval.freq)
help(points)
points(interval.freq)
barplot(interval.freq)
hist(faithful$eruptions, right=FALSE)
duration = faithful$eruptions
hist(duration, right=FALSE)
colors = c("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan")
hist(duration, right=FALSE, col=colors, main="Old Faithful Eruptions", xlab="Duration minutes")
waiting_period = faithful$waiting
waiting_period = faithful$waiting
hist(waiting_period, right=FALSE, col=colors, main="Old Faithful Eruptions", xlab="Interval")
hist(waiting_period, right=FALSE, col=colors, main="Old Faithful Eruptions", xlab="Interval in minutes")
q()
duration = faithful$eruptions
breaks = seq(1.5, 5.5, by=0.5)
duration.cut = cut(duration, breaks, right=FALSE)
duration.freq = table(duration.cut)
duration.relfreq = duration.freq / nrow(faithful)
duration.relfreq
pie(duration.relfreq)
hist(duration.relfreq)
hist(duration.relfreq)
duration.freq
duration = faithful$eruptions
breaks = seq(1.5, 5.5, by=0.5)
breaks
typeof(breaks)
duration.cut = cut(duration, breaks, right=FALSE)
duration.freq = table(duration.cut)
duration.cumfreq = cumsum(duration.freq)
duration.cumfreq
hist(cbind(duration.cumfreq))
interval = faithful$waiting
help(function)
help("function")
help("function")
calc_cumfreq <- function(records)
breaks = seq(1.5, 5.5, by=0.5)
records.cut = cut(duration, breaks, right=FALSE)
records.freq = table(duration.cut)
records.cumfreq = cumsum(records.freq)
return(records)
calc_cumfreq <- function(records)
breaks = seq(1.5, 5.5, by=0.5)
records.cut = cut(records, breaks, right=FALSE)
help("function")
duration.cumfreq
interval = faithful$eruptions
breaks
breaks.cut = cut(interval, breaks, right=FALSE)
interval.freq = table(interval.cut)
interval.cumfreq = cumsum(interval.freq)
hist(interval.cumfreq)
q()
duration = faithful$eruptions
breaks = seq(1.5, 5.5, by=0.5)
duration.cut = cut(duration, breaks, right=FALSE)
duration.freq = table(duration.cut)
cumfreq0 = c(0, cumsum(duration.freq))
plot(breaks, cumfreq0, main="Old Faithful Eruptions", xlab="Duration minutes", ylab="Cumulative Eruptions")
lines(breaks, cumfreq0)
intervals = faithful$waiting
faithful$waiting
cbind(faithful$waiting)
faithful$waiting / nrow(faithful)
faithful$waiting / nrow(faithful$waiting)
intervals
breaks
intervals.cut = cut(breaks, intervals)
help(cut)
intervals.cut = cut(intervals, breaks)
intervals.cut
intervals.cut
intervals.freq = table(intervals.cut)
intervals.cut = cut(intervals, breaks, right=FALSE)
intervals.freq = table(intervals.cut)
cumfreq0 = c(0, cumsum(intervals.freq))
cumfreq0
intervals
intervals.cut = cut(intervals, breaks, right=FALSE)
intervals.cut
intervals.freq = table(intervals.cut)
intervals.freq
cbind(intervals.freq)
plot(breaks, cumfreq0)
intervals
cut(intervals, breaks)
cut(intervals, breaks, right=FALSE)
duration
cut(duration, breaks)
breaks = seq(0, 100, by=10)
breaks
cut(intervals, breaks)
intervals.cut = cut(intervals, breaks)
intervals.cut = cut(intervals, breaks, right=FALSE)
intervals.cut
intervals.freq = table(intervals.cut)
cumfreq0 = c(0, cumsum(intervals.freq))
cumfreq0
plot(breaks, cumfreq0)
lines(breaks, cumfreq0)
intervals
min(intervals)
max(intervals)
breaks = seq(40.0, 100.0, by=5.0)
intervals.cut = cut(intervals, breaks, right=FALSE)
intervals.cut
floor?
help(floor)
intervals.freq = table(intervals.cut)
intervals.freq
cumfreq0 = c(0, cumsum(intervals.freq))
cumfreq0
plot(breaks, cumfreq0)
lines(breaks, cumfreq0)
plot(breaks, cumfreq0, main="Old Faithful Intervals", xlab="Interval minutes", ylab="Cumulative Intervals")
lines(breaks, cumfreq0)
q()
duration = faithful$eruptions
breaks = seq(1.5, 5.5, by=0.5)
duration.cut = cut(duration, breaks, right=FALSE)
duration.freq = table(duration.cut)
duration.cumfreq = cumsum(duration.freq)
duration.cumrelfreq = duration.cumfreq / nrow(faithful)
duration.cumrelfreq
old = options(digits=2)
duration.cumrelfreq
options(old)
duration.cumrelfreq
cbind(duration.cumfreq, duration.cumrelfreq)
interval = faithful$waiting
min(interval)
max(interval)
ceiling(max(interval))
help(ceiling)
interval * 0.10
max(interval) * 0.1)
ceiling(max(interval) * 0.1)
interval_breaks = seq(floor(min(interval) * 0.1) * 10.0, ceiling(max(interval) * 0.1) * 10.0, right=FALSE)
interval_breaks
interval_breaks = seq(floor(min(interval) * 0.1) * 10.0, ceiling(max(interval) * 0.1) * 10.0, by=nrow(interval) / 10.0, right=FALSE)
interval_breaks = seq(floor(min(interval) * 0.1) * 10.0, ceiling(max(interval) * 0.1) * 10.0, by=(nrow(faithful) / 10.0), right=FALSE)
interval_breaks
interval_breaks = seq(floor(min(interval) * 0.1) * 10.0, ceiling(max(interval) * 0.1) * 10.0, by=(nrow(faithful) / 5.0), right=FALSE)
interval_breaks
interval_breaks = seq(floor(min(interval) * 0.1) * 10.0, ceiling(max(interval) * 0.1) * 10.0, by=(nrow(faithful) / 20.0), right=FALSE)
interval_breaks
interval_breaks = seq(floor(min(interval) * 0.1) * 10.0, ceiling(max(interval) * 0.1) * 10.0, by=(nrow(faithful) / 30.0), right=FALSE)
interval_breaks
interval_breaks = seq(floor(min(interval) * 0.1) * 10.0, ceiling(max(interval) * 0.1) * 10.0, by=5.0 right=FALSE)
interval_breaks = seq(floor(min(interval) * 0.1) * 10.0, ceiling(max(interval) * 0.1) * 10.0, by=5.0, right=FALSE)
interval_breaks
length(interval_breaks)
type(interval_breaks)
typeof(interval_breaks)
interval
interval.freq
interval.cut = cut(interval, interval_breaks, right=FALSE)
interval.cut
interval.freq = table(interval.cut)
interval.cumfreq = cumsum(interval.freq)
interval.cumfreq
interval.cumrelfreq = interval.cumfreq / nrow(faithful)
interval.cumrelfreq
cbind(interval.cumfreq, interval.cumrelfreq)
q()
c(duration.freq)
duration.freq)
duration.freq
c(0, duration.freq)
cumfreq0
cumfreq0 = c(0, cumsum(duration.freq))
cumfreq0
cumrelfreq0 = cumfreq0 / nrow(faithful)
cumrelfreq0
breaks
plot(breaks, cumrelfreq0, main="Old Faithful Eruptions", xlab="Duration minutes", ylab="Cumulative Eruptions Proportion")
lines(breaks, cumrelfreq0)
library(actuar)
help("library")
library(actuar)
help(ogive)
Fn = ogive(breaks, duration.freq)
plot(Fn, main="Old Faithful Eruptions", xlab="Duration minutes", ylab="Cumulative Relative Frequency")
library(PBSModelling)
library(PBSModeling)
library(PBSmodelling)
plotCsum(duration)
intervals
intervals.cut
interval_breaks
interval
intervals
interval.cut
interval.freq
intervalFn = ogive(breaks, interval.freq)
intervalFn = ogive(interval_breaks, interval.freq)
plot(intervalFn, main="Old Faithful Eruptions", xlab="Intervals in minutes", ylab="Cumulative Relative Frequency")
plotCsum(interval)
q()
duration
stem(duration)
interval
stem(interval)
interval
help(stem)
q()
waiting = faithful$waiting
head(cbind(duration, waiting))
plot(duration, waiting, xlab="Eruption duration", ylab="Time waited")
par()
plot(1:10, 1:10, type="n")
help("font")
??font
# the above was a brief excursion into font settings and such
abline(lm(waiting - duration))
plot(duration, waiting, xlab="Eruption duration", ylab="Time waited")
abline(lm(waiting - duration))
abline(lm(waiting ~ duration))
q()
mean(duration)
mean(interval)
??points
median(duration)
median(interval)
quantile(duration)
quantile(interval)
quantile(duration, c(.32, .57, .98))
quantile(interval, c(.17, .43, .67, .85))
q()
q()
max(duration)
min(duration)
max(interval) - min(interval)
IQR(duration)
IQR(interval)
boxplot(duration, horizontal=TRUE)
boxplot(interval, horizontal=TRUE)
q
q()
var(duration)
var(interval)
max(interval) - min(interval)
sd(duration)
sd(interval)
waiting
interval == waiting
cov(duration, waiting)
q()

Binary file not shown.

View File

@ -0,0 +1,146 @@
c(2, 3, 5)
length(c("aa", "bb", "cc", "dd", "ee"))
n = c(2, 3, 5)
s = c("aa", "bb", "cc", "dd", "ee")
c(n, s)
a = c(1, 3, 5, 7)
b = c(1, 2, 4, 8)
5 * a
a + b
a - b
a * b
u = c(10, 20, 30)
v = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
u + v
a[0]
a[1]
b
b[0]
b[1]
b
b[-2]
b[20]
b[c(2, 3)]
a
a[c(1, 4)]
a
a[1:4]
a[1:3]
a[c(FALSE, TRUE, FALSE, TRUE)]
v = c("Mary", "Sue")
names(v)
names(v) = c("First", "Last")
names(v)
v["First"]
A = matrix(
c(2, 4, 3, 1, 5, 7),
nrow=2,
ncol=3,
byrow=TRUE)
A
A[1, 3]
A[2,]
A[, c(1, 3)]
dimnames(A) = list(
c("row1", "row2"),
c("col1", "col2", "col3"))
A
A["row2", "col3"]
B = matrix(
c(2, 4, 3, 1, 5, 7),
nrow=3,
ncol=2)
B
t(B)
C = matrix(
c(7, 4, 2),
nrow=3,
ncol=1)
C
cbind(B, C)
D = matrix(
c(6, 2),
nrow=1,
ncol=2)
D
B
BD = rbind(B, D)
BD
c(B)
n = c(2, 3, 5)
s = c("aa", "bb", "dd", "ee")
b = c(TRUE, FALSE, TRUE, FALSE, FALSE)
x = list(n, s, b, 3)
x
x[2]
x[1]
x[2]
x[[2]]
x[[2]][1] = "DORP"
x[[2]]
s
v = list(bob=list("Bob", "Johnson", c(2, 3, 5)), john=list("John", "Smythe", c(1, 4, 6)))
v
v["bob"]
v[["bob"]]
v[["bob"]][[1]]
v[["bob"]][[1]] = "Robert"
v["bob"]
v$bob
v$john
v$john[1]
v$john[[1]]
v$john[[1]] =
"Johnny"
v$john
attach(v)
bob
detach(v)
bob
mtcars
n = c(2, 3, 5)
s = c("aa", "bb", "cc")
b = c(TRUE, FALSE, TRUE)
df = data.frame(n, s, b)
mtcars["Mazda RX4", "mpg"]
nrow(mtcars)
ncol(mtcars)
ncol(s)
s
B
nrow(B)
ncol(B)
head(mtcars)
q()
s
q()
mtcars[[[9]]
mtcars[[9]]
mtcars[["am]]
mtcars[["am"]]
mtcars$am
mtcars[1]
head(mtcars[1])
mtcars["mpg"]
mtcars[c("mpg", "hp")]
mtcars[c("mpg", "hp")][1]
mtcars[c("mpg", "hp")][[1]]
mtcars[24,]
mtcars["Camaro Z28",]
mtcars[c("Datsun 710", "Camaro Z28")]
mtcars[c("Datsun 710", "Camaro Z28",)]
mtcars[c("Datsun 710", "Camaro Z28"),]
L = mtcars$am == 0
L
mtcars[L,]
mtcars[L,]
mtcars[L,]$mpg
library(gdata)
library(foreign)
help(read.mtp)
mydata = read.table("mydata.txt")
mydata
mydata = read.csv("mydata.csv")
mydata
getwd()
q()

View File

@ -0,0 +1,4 @@
Col1,Col2,Col3
100,a1,b1
200,a2,b2
300,a3,b3
1 Col1 Col2 Col3
2 100 a1 b1
3 200 a2 b2
4 300 a3 b3

View File

@ -0,0 +1,4 @@
100 a1 b1
200 a2 b2
300 a3 b3
400 a4 b4

View File

@ -0,0 +1,12 @@
# Yes, this is just a test to make sure I'm remotely
# capable of writing and running an R file.
print("dogs rule cats drool")
x = 10.5
y = as.integer(4)
print(x + y)
# vim:filetype=r