Skip to content

Commit 6d7396c

Browse files
author
algorithmica-repository
committed
Uploading all the class examples
1 parent 9d98807 commit 6d7396c

File tree

18 files changed

+1070
-0
lines changed

18 files changed

+1070
-0
lines changed

1.introduction/commands.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
help related commands
2+
--------------------
3+
help.start()
4+
help(name) / ?name
5+
example(name)
6+
7+
workspace commands
8+
------------------
9+
ls()
10+
ls(pattern="")
11+
rm(object name)
12+
data()
13+
source("script-file")
14+
getwd(), setwd()
15+
save.image()
16+
17+
history commands
18+
---------------
19+
history()
20+
history(max.show=Inf)
21+
savehistory(file="")
22+
loadhistory(file="")
23+
24+
packages commands
25+
---------------------
26+
install.packages("packagename")
27+
install.packages("packagename",repos="")
28+
library(packagename)
29+
library() -- shows all packages installed on machine(may or maynot be loaded into R workspace)
30+
search() -- show all the packages loaded into R workspace and also shows the search path order
31+
32+
33+
common functions
34+
---------------------
35+
length(object) # number of elements or components
36+
str(object) ���# structure of an object
37+
class(object) �# class or type of an object
38+
names(object) �# names
39+
40+
head(mydata, n=10)/head(mydata, 10)
41+
42+
tail(mydata, n=5)/tail(mydata, 5)
43+
colnames(mydata)
44+
rownames(mydata)
45+
summary(mydata)
46+
47+
summary functions
48+
----------------
49+
mean(x)/mean(x, na.rm=TRUE)
50+
median(x)
51+
table(x)
52+
var(x)
53+
sd(x)
54+
max(x)-min(x)
55+
IQR(x)
56+
quantile(x)

2.datastructures/datastructures1.R

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
a = 10
2+
b = 20.3
3+
c = a + b
4+
a=20
5+
ls()
6+
rm("b")
7+
8+
v1 = 1:1000
9+
v1
10+
11+
v = c(10,20,15,22,8,2)
12+
v[1:3]
13+
v[c(3,5)]
14+
v[v>15]
15+
v[-c(1,2)]
16+
which(v>15)
17+
18+
v2=1:6
19+
v3=v+v2
20+
21+
v4 = v * v2
22+
23+
sd(v)
24+
length(v)
25+
v2 = sort(v)
26+
27+
v3=seq(1,100,10)
28+
29+
v4=rep(1,10)
30+
31+
v5 = c("aa","bb","ccc")
32+
33+
students = read.csv("E:/data analytics/datasets/students.csv",TRUE)
34+
students
35+
36+
class(students)
37+
rn = c("user1","user2")
38+
cn = c("movie1","movie2","movie3")
39+
40+
user_movies_ratings=matrix(1:6,2,3,dimnames=list(rn,cn))
41+
user_movies_ratings1=rbind(user_movies_ratings,c(5,2,5))
42+
43+
44+
row.names(user_movies_ratings) =c("user1","user2","user3")
45+
user_movies1=matrix(rep(1,6),2,3)
46+
dimnames(user_movies1)=list(rn,cn)
47+
dim(user_movies_ratings)
48+
nrow(user_movies_ratings)
49+
t(user_movies_ratings)
50+
51+
user_movies_ratings2=matrix(1:6,3,2)s
52+
res = user_movies_ratings1 %*% user_movies_ratings2
53+
diag(3)
54+
55+
v1=1:3
56+
dim(v1)
57+
length(v1)
58+
v1=as.matrix(v1)
59+
dim(v1)
60+
61+
m1=matrix(1:6,2,3,TRUE,)
62+
m1
63+
m2=matrix(1:6,2,3)
64+
m2
65+
66+
d = c(1,2,3,4)
67+
e = c("red", "white", "red", NA)
68+
f = c(TRUE,TRUE,TRUE,FALSE)
69+
mydata = data.frame(d,e,f)
70+
names(mydata) = c("ID","Color","Passed")
71+
72+
f=factor(c("y","n"))
73+
f

2.datastructures/datastructures2.R

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
3+
### Let's create some data!
4+
5+
6+
## Vector
7+
8+
vec_1 <- c(1,2,3,4,5,6)
9+
vec_1
10+
11+
vec_2 <- 1:6
12+
vec_2
13+
14+
vec_3 <- c("One", "Two", "Three", "Four", "Five", "Six")
15+
vec_3
16+
17+
vec_4 <- 1:500
18+
vec_4
19+
20+
21+
## Matrix
22+
23+
mat_1 <- matrix(1:16, nrow = 4, ncol = 4)
24+
mat_1
25+
26+
mat_2 <- matrix(vec_1, nrow = 3, ncol = 2, byrow = TRUE)
27+
mat_2
28+
29+
mat_3 <- matrix(vec_1, nrow = 3, ncol = 2, byrow = FALSE)
30+
mat_3
31+
32+
rows <- c("A", "B", "C")
33+
cols <- c("AA", "BB")
34+
35+
mat_4 <- matrix(vec_1, nrow = 3, ncol = 2, dimnames = list(rows, cols))
36+
mat_4
37+
38+
rownames(mat_3) <- rows # rename rows afterwards
39+
mat_3
40+
41+
## Array
42+
43+
array_1 <- array(1:18, c(3,2,3))
44+
array_1
45+
46+
dims <- c("AAA", "BBB", "CCC")
47+
array_2 <- array(1:18, c(3, 2, 3), dimnames = list(rows, cols, dims))
48+
array_2
49+
50+
51+
## Factor
52+
53+
vec_1
54+
vec_4 <- c("odd", "even", "odd", "even", "odd", "even")
55+
vec_4
56+
57+
fac_1 <- factor(vec_4)
58+
fac_1
59+
as.numeric(fac_1) # Levels are created alphabetiacal
60+
61+
vec_5 <- c("small", "small", "medium", "medium", "high", "high")
62+
vec_5
63+
64+
fac_2 <- factor(vec_5, ordered = TRUE)
65+
fac_2 # look what happened to the Levels output!
66+
as.numeric(fac_2) # still alphabetical
67+
68+
fac_3 <- factor(vec_5, ordered = TRUE, levels = c("small", "medium", "high"))
69+
fac_3
70+
as.numeric(fac_3) # Yeah!
71+
72+
73+
## Data Frame
74+
75+
df_1 <- data.frame(vec_1, vec_2, vec_3, fac_1, fac_3)
76+
df_1
77+
str(df_1) # shows structure - data.frame() usually converts characters into factors!
78+
79+
names(df_1) <- c("var_1", "var_2", "var_3", "var_4", "var_5")
80+
df_1
81+
82+
83+
## List
84+
85+
list_1 <- list(vec_1, vec_2, vec_3)
86+
list_1
87+
88+
list_2 <- list(vector_1 = vec_1, vector_2 = vec_2, vector_3 = vec_3)
89+
list_2
90+
91+
list_3 <- list(text = "Sample text", vector = vec_1, matrix = mat_2, array = array_2, factor = fac_3, data_frame = df_1)
92+
list_3
93+
94+
95+
### saving all data
96+
save.image(file="data.RData")

2.datastructures/datastructures3.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
## Vector
3+
4+
vec_1 # whole vector
5+
vec_1[1] # 1st value
6+
vec_1[c(1,4)] #1st and 2nd value
7+
vec_1[1:4] # 1st thru 4th value
8+
9+
## Matrix
10+
11+
mat_1
12+
mat_1[1,] # 1st row
13+
mat_1[,1] # 1st column
14+
mat_1[1,1] # "1st" cell
15+
16+
mat_4
17+
mat_4[c("A"),] # row "A"
18+
19+
20+
## Array
21+
22+
array_2
23+
array_2[,,2] #2nd dimension
24+
array_2[,,c("BBB")] # same as above
25+
array_2[2,,]
26+
27+
## Data Frame
28+
29+
df_1
30+
df_1[1,] #1st row - see Matrix
31+
df_1$var_2 # coloumn/variable named "var_2"
32+
df_1$var_3 # coloumn/variable named "var_3"
33+
df_1[,3] # 3rd column
34+
35+
## List
36+
37+
list_3
38+
list_3[[3]] # selects the matrix
39+
list_3[3] # selects the matrix, but stays as list
40+
41+
list_3$factor # selects list entry "factor"
42+
list_3$data_frame # selects list entry "data_frame"
43+
44+
# why you should use double brackets
45+
test_1 <- list_3[[3]]
46+
test_2 <- list_3[3]
47+
test_1
48+
test_2
49+
is.matrix(test_1) # is matrix
50+
is.list(test_2) # is list
51+
52+
list_3[[c("matrix")]] # works also
53+
list_3$matrix # works also
54+
55+
56+
## nesting
57+
58+
list_3$data_frame$var_4 # selects column "var_4" in list entry "data_frame"
59+
list_3[[6]]$var_4 # same as above
60+
list_3[[c("data_frame")]]$var_4 # same as above
61+
62+
63+
## different ways to access data
64+
65+
array_2
66+
array_2[1,2,2]
67+
array_2[,,2][1,2] # same as above

3.eda-stats/eda1.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
students = read.csv("E:/data analytics/datasets/students.csv",TRUE)
2+
class(students)
3+
dim(students)
4+
names(students)
5+
str(students)
6+
head(students,10)
7+
tail(students)
8+
summary(students)
9+
summary(students[,"Height"])
10+
summary(students$Height)

3.eda-stats/eda2.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
library(dplyr)
2+
3+
students=read.csv("E:/data analytics/datasets/students.csv")
4+
d=dim(students)
5+
typeof(d)
6+
str(students)
7+
head(students,10)
8+
tail(students)
9+
summary(students)
10+
mean(students$Height)
11+
median(students$Height)
12+
mean(c(46,37,40,33,42,36,40,47,34,45))
13+
sd(students$Height)
14+
mad(students$Height)
15+
IQR(students$Height)
16+
mean(students$MilesHome)
17+
mean( students$MilesHome, na.rm = TRUE)
18+
class(students)
19+
20+
students1 = filter(students, Sleep %in% c(6,8) & BloodType=="O")
21+
students2 = select(students1,Height)
22+
23+
students %>%
24+
filter(Sleep %in% c(6,8) & BloodType=="O") %>%
25+
arrange(Height) %>% mutate(Family=Brothers + Sisters) %>%
26+
summarise(n())
27+
28+
by.major = group_by(students, Major)
29+
class(by.major)
30+
summarise(by.major, count=n())
31+
32+
33+
34+
35+

4.eda-graphics/eda-graphics.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library(ggplot2)
2+
3+
students=read.csv("E:/data analytics/datasets/students.csv")
4+
dim(students)
5+
summary(students$Sex)
6+
table(students$Level)
7+
with(students, table(Level))
8+
9+
ggplot(students, aes(x = BloodType)) + geom_bar()
10+
with(students, table(Sex, Level))
11+
ggplot(students, aes(x = Level, fill = BloodType)) + geom_bar(position = "dodge")`-
12+
13+
ggplot(students, aes(x = Height)) + geom_dotplot()
14+
15+
16+

0 commit comments

Comments
 (0)