forked from decisionlabiitk/BSE658_chapter1
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathNotebook for chapter 1_part 2.Rmd
134 lines (97 loc) · 3.15 KB
/
Notebook for chapter 1_part 2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
title: "R Notebook"
output: html_notebook
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
# Special values:
1. Inf: stands for Infinity.
```{r}
v <- 1/0
v
```
2. NaN: Not a Number. A reserved keyword to represent a quantity that
cannot be mathematically defined.
```{r}
v <- 0/0
v
```
3. NA: not available. Used when the data is missing or unavailable in a dataset.
4. NULL: No value.
For NaN we actually know what the value is, because it’s something insane like 0/0.
For NA, we believe that there is supposed to be a value “out there”, but we don’t quite know what it is.
But for NULL we strongly believe that there is no value at all.
# Factor:
Say 5 students just got their marks cards and the scores were as follows:
```{r}
marks <- c(24, 30, 35, 19, 20)
```
None of them were happy and so they opted for a re-exam. Their marks doubled.
```{r}
reexam_marks <- marks*2
```
Now let's say there are three groups that got three different treatments
I'll label them
```{r}
treatmentGroup <- c(1,1,2,2,3,3)
class(treatmentGroup)
```
Now if I attempt to do some operations like
```{r}
treatmentGroup + 1
```
And we know this makes no sense given treatment group is a nominal variable
But how do we make R make sense of this?
```{r}
treatmentGroup <- as.factor(treatmentGroup)
treatmentGroup
```
```{r}
class(treatmentGroup)
```
```{r}
treatmentGroup + 1
```
# Lists
Lists are like dataframes but are even more flexible.
Data frames are nice “rectangular” table of data. But Lists have no such constraints.
```{r}
Dan <- list( age = 34,
nerd = TRUE,
parents = c("Joe","Liz")
)
print(Dan)
```
# Loops
Sometimes you want to repeat a line of code multiple times
A) A WHILE loop:
Say, you want to find the smallest multiple of 17 that is greater than or equal to 1000.
Here’s how you could do this:
```{r}
x <- 0
while ( x < 1000 ) {
x <- x + 17
}
print( x )
```
B) A FOR loop:
```{r}
for ( i in 1:3 ) {
print( "hello" )
}
```
# Conditional:
Say if a condition holds (student belongs to IITK), then you ask something (their feedback related to IITK) , else you dont ask anything. How can you do that?
```{r}
today <- Sys.Date() # pull the date from the system clock
day <- weekdays( today ) # what day of the week is today
# now make a choice depending on the day...
if ( day == "Monday" ) {
print( "Yes I have class today" )
} else {
print( "No class today! Yay!" )
}
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.