Skip to content

Commit e24045d

Browse files
author
jonsan21
committed
Slides and script
1 parent 333f1d1 commit e24045d

File tree

8 files changed

+384
-0
lines changed

8 files changed

+384
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#Loops in R
2+
3+
1:3
4+
for(i in 1:3) {
5+
cat(i, "+", i, "=", i+i, "\n")
6+
}
7+
d <- data.frame(a = 1:2, b=2:3)
8+
9+
for(x in d) {
10+
cat("Column sum:", sum(x), "\n")
11+
}
12+
13+
#Flow Control: if and if else statements
14+
15+
for(i in 1:3){
16+
if (i==2) cat("This index is even:","\n")
17+
cat(i,"\n")
18+
}
19+
20+
for(i in 1:3){
21+
if (i==2) cat("The index is 2","\n") else
22+
cat("The index is not 2","\n")
23+
}
24+
25+
#While loop example
26+
27+
28+
set.seed(886)
29+
30+
k<-0 # number of big parts (>2)
31+
y<-abs(rnorm(1000)) # simulated part size
32+
i<-0 # index of parts
33+
# loop:
34+
while(k<3 & i<1000){
35+
i<-i+1
36+
temp<-y[i]
37+
k<-k+(temp>2)
38+
}
39+
rm(temp)
40+
i
41+
42+
#Repeat loop example}
43+
44+
set.seed(371)
45+
eye.colors<-c("brown","blue","green","yellow","grey")
46+
eyecolor<-data.frame(personId=1:100,color=
47+
sample(eye.colors,100,rep=T))
48+
i<-0
49+
list.of.ids<-numeric(0) # patient ID list
50+
#loop:
51+
repeat {
52+
i<-i+1
53+
if(eyecolor$color[i]=="yellow" |
54+
eyecolor$color[i]=="blue") next
55+
list.of.ids<-c(list.of.ids,eyecolor$personId[i])
56+
if(i==100 | length(list.of.ids)==20) break
57+
}
58+
list.of.ids
59+
60+
#Loops and run times
61+
62+
set.seed(853)
63+
y<-matrix(rnorm(1000000),nrow=1000)
64+
z<-0*y
65+
time1<-as.numeric(Sys.time())
66+
#loop:
67+
for(i in 1:1000){
68+
for(j in 1:1000){
69+
z[i,j]<-y[i,j]^2
70+
}
71+
}
72+
time2<-as.numeric(Sys.time())
73+
74+
# using object form in R:
75+
z<-y^2
76+
time3<-as.numeric(Sys.time())
77+
78+
# run time increase factor:
79+
(time2-time1)/(time3-time2)
80+
81+
#lapply() and sapply()
82+
83+
set.seed(777)
84+
85+
my.data<-data.frame(data1=rnorm(10),data2=rnorm(10),data3=rnorm(10))
86+
lapply(my.data,sum)
87+
sapply(my.data,sum)
88+
89+
#lapply() and sapply() examples
90+
91+
A<-matrix(1:9,nrow=3);B<-matrix(1:16,nrow=4);C=matrix(1:8,nrow=4)
92+
my.list<-list(A=A,B=B,C=C); my.list
93+
94+
lapply(my.list,"[",,2)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#Constructing vectors
2+
3+
x<-9:17
4+
x
5+
6+
y<-seq(0,1,length=11)
7+
y
8+
9+
z<-rep(1:2, 5)
10+
z
11+
12+
xz10<-c(x,z,10)
13+
xz10
14+
15+
#Constructing matrices
16+
17+
A<-rbind(1:3, c(1,1,2))
18+
A
19+
20+
B<-cbind(1:3, c(1,1,2))
21+
B
22+
23+
C<-matrix(c(1,0,0,1,1,0,1,1,1), nrow=3, ncol=3)
24+
C
25+
26+
#Index and logical index
27+
28+
x<- (-5):5
29+
x
30+
31+
x[4:8]
32+
33+
x[-c(1:3,9:11)]
34+
35+
index<-abs(x)<3
36+
index
37+
x[index]
38+
39+
A<-matrix((-4):5, nrow=2, ncol=5)
40+
A
41+
A[A<0]
42+
43+
A[A<0]<-0
44+
A
45+
46+
A[2,]
47+
48+
A[,c(2,4)]
49+
50+
#Properties of vectors and matrices
51+
52+
A<-matrix(rep(c(TRUE,FALSE),2),nrow=2)
53+
B<-rnorm(4)
54+
C<-matrix(LETTERS[1:9],nrow=3)
55+
A;B;C
56+
mode(A);mode(B);mode(C)
57+
58+
x<-matrix(c(NA,2:12),ncol=3)
59+
x
60+
length(x[1,])
61+
length(x)
62+
63+
dim(x); dim(x)[2]
64+
65+
#Naming rows and columns in a matrix
66+
67+
x<-matrix(rnorm(12),nrow=4)
68+
x
69+
dimnames(x)[[2]]<-paste("data",1:3,sep="")
70+
dimnames(x)[[1]]<-paste("obs",1:4,sep="")
71+
x
72+
73+
rownames(x)
74+
colnames(x)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#Reading data from a text file
2+
3+
mydat<-data.frame(x1=c(2,2,3,3,1,1), x2=c(.3, 1, 2.1, 2.2, .1, .2), x3=0.01*c(1,11,4,2,10,6))
4+
mydat
5+
6+
#The R working directory
7+
8+
getwd()
9+
setwd("c:/otherdatadir")
10+
mydat<-read.table("Data/filename.mydat", header=TRUE)
11+
12+
# The read.table()| function
13+
14+
options(deparse.cutoff=48)
15+
args(read.table)
16+
dat<-read.table("Data/testdat1.dat", header=TRUE, skip=5, nrow=2)
17+
dat
18+
19+
dat<-read.table("Data/testdat2.dat", header=TRUE, na.strings=".",
20+
comment.char=";", dec=",")
21+
dat
22+
23+
#Reading from more complicated files
24+
25+
cat(1:5+pi,'\n',file="scantest.txt")
26+
27+
vec<-scan("scantest.txt")
28+
vec
29+
30+
cat(paste("A B C\n",paste(1:5+0.324654, collapse=" "),'\n',"How many roads\n", sep=""),file="readlinestest.txt")
31+
32+
vec<-readLines("readlinestest.txt")
33+
vec
34+
strsplit(vec[2]," ")
35+
as.numeric(strsplit(vec[2]," ")[[1]])
36+
37+
#File connections
38+
39+
f1<-file("readlinestest.txt", open="r")
40+
scan(f1,what="",nlines=1)
41+
scan(f1,what=double(),nlines=1)
42+
readLines(f1)
43+
close(f1)
177 KB
Binary file not shown.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#Writing data to a text file}
2+
3+
mydat<-data.frame(x1=c(2,2,3,3,1,1),
4+
x2=c(0.3,1,2.1,2.2,0.1,0.2),
5+
x3=c(0.01,0.11,0.04,0.02,0.1,0.06))
6+
mydat
7+
8+
write.table(mydat,file="c:/datadir/filename.dat"),
9+
row.names=FALSE,col.names=FALSE,sep=", ")
10+
11+
write.table(mydat,file="Data/write.datatest.txt",
12+
row.names=FALSE,col.names=FALSE,sep=", ")
13+
14+
#Basic writing functions
15+
16+
set.seed(832)
17+
cat("Test file for cat\n",round(rnorm(5),3),"\n",
18+
file="cattest.txt")
19+
20+
lin<-c("Count down", paste(rev(1:10), collapse="-"),
21+
"Go")
22+
writeLines(lin, con="Data/writelinestest.txt")
23+
24+
#A few special ones
25+
26+
sink("Data/sinktest.txt")
27+
x<-1:5
28+
y<-1:3
29+
outer(x,y)
30+
sink()
31+
32+
set.seed(543)
33+
x<-1:3
34+
y<-rpois(10, 4)
35+
dump(c("x","y"), file="Data/dumptest.txt")
36+
37+
lis<-list(x=1:5, y=3, z=c("a","b","c"))
38+
dput(lis, file="Data/dputtest.txt")
39+
dget("Data/dputtest.txt")
40+
41+
#Using file connections}
42+
43+
set.seed(713)
44+
f2<-file("Data/testout.txt", open="w")
45+
cat("Header of file\n\n", file=f2)
46+
mat<-matrix(round(rnorm(12),8), ncol=3)
47+
write.table(mat, file=f2, row.names=FALSE, col.names=FALSE)
48+
close(f2)
49+
50+
#Using append=TRUE
51+
52+
set.seed(484)
53+
54+
cat("Header of file\n\n", file="Data/testappend.txt")
55+
mat<-matrix(round(rnorm(12),8), ncol=3)
56+
write.table(mat, file="Data/testappend.txt", row.names=FALSE,
57+
col.names=FALSE, append=TRUE)
58+
59+
#Working with binary files: Using save() and load()
60+
61+
x<-rnorm(3)
62+
lis<-list(y=1:5, z="lalala", fun=function()cat("ha-ha-ha\n"))
63+
save(x,lis, file="Data/test1.RData")
64+
65+
rm(list=ls())
66+
load(file="Data/test1.RData")
67+
ls()
68+
174 KB
Binary file not shown.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#Reading from SQL databases}
2+
3+
library(RODBC)
4+
connStr <- paste(
5+
"Server=msedxeus.database.windows.net",
6+
"Database=DAT209x01",
7+
"uid=RLogin",
8+
"pwd=P@ssw0rd",
9+
"Driver={SQL Server}",
10+
sep=";"
11+
)
12+
13+
if(.Platform$OS.type != "windows"){
14+
connStr <- paste(
15+
"Server=msedxeus.database.windows.net",
16+
"Database=DAT205x01",
17+
"uid=PBIlogin",
18+
"pwd=P@ssw0rd",
19+
"Driver=FreeTDS",
20+
"TDS_Version=8.0",
21+
"Port=1433",
22+
sep=";"
23+
)
24+
}
25+
26+
conn <- odbcDriverConnect(connStr)
27+
28+
\end{itemize}
29+
\end{itemize}
30+
\end{frame}
31+
32+
33+
\begin{frame}[fragile]%%\linespread{0.9}
34+
\frametitle{Connecting to a local SQL Database on your harddisk:}
35+
\begin{itemize}
36+
\item Replace server name with the SQL server name on the local machine;
37+
38+
\item With the default SQL installation, this is equal to the {\bf name of the local machine}:
39+
40+
41+
\begin{Sinput}
42+
>connStr <- paste(
43+
+ "Server=My_Machine",
44+
+ "Database=DAT205x01",
45+
+ "uid=PBIlogin",
46+
+ "pwd=P@ssw0rd",
47+
+ "Driver={SQL Server}",
48+
+ sep=";"
49+
+ )
50+
51+
connStr <- paste(
52+
"Server=msedxeus.database.windows.net",
53+
"Database=DAT209x01",
54+
"uid=PBIlogin",
55+
"pwd=P@ssw0rd",
56+
"Driver=FreeTDS",
57+
"TDS_Version=8.0",
58+
"Port=1433",
59+
sep=";"
60+
)
61+
62+
conn <- odbcDriverConnect(connStr)
63+
64+
#A first query
65+
66+
tab <- sqlTables(conn)
67+
head(tab)
68+
69+
#Getting a table
70+
71+
mf <- sqlFetch(conn,"bi.manufacturer")
72+
mf
73+
74+
#Submit real SQL
75+
76+
query <- "
77+
SELECT Manufacturer
78+
FROM bi.manufacturer
79+
WHERE ManufacturerID < 10
80+
"
81+
sqlQuery(conn, query)
82+
83+
#Large tables
84+
85+
sqlQuery(conn, "SELECT COUNT(*) FROM bi.salesFact")
86+
sqlColumns(conn,"bi.salesFact")[c("COLUMN_NAME","TYPE_NAME")]
87+
sqlQuery(conn, "SELECT TOP 2 * FROM bi.salesFact")
88+
89+
df <- sqlQuery(conn, "SELECT * FROM bi.salesFact WHERE Zip='30116'")
90+
dim(df)
91+
92+
sapply(df, class)
93+
94+
#SQL summary statistics
95+
96+
df <- sqlQuery(conn,
97+
"SELECT AVG(Revenue), STDEV(Revenue), Zip
98+
FROM bi.salesFact
99+
GROUP BY Zip"
100+
)
101+
colnames(df) <- c("AVG(Revenue)", "STDEV(Revenue)", "Zip")
102+
103+
close(conn)
104+
105+
209 KB
Binary file not shown.

0 commit comments

Comments
 (0)