generated from rstudio/bookdown-demo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path03-rprogramming.Rmd
169 lines (113 loc) · 2.75 KB
/
03-rprogramming.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# R Programming Techniques
## R Programming Essential Tips
1. When repeating mutate to create for example percentage column in a table simply write that as a pip compatible function and call the function.
2. Also, write small chucks of code and run it to see if it works. It will be much easier to debug if there is an error and to find and isolate it. It becomes very difficult to debug code the bigger it gets.
## Basics of R
### Print a string.
```{r, echo=TRUE, eval=TRUE}
print("First R lesson.")
```
### print a integer Value.
```{r}
print(23)
```
### Print with variable.
```{r}
x <- "First R lesson."
x
```
### Print String with varibale.
```{r}
x <- "First"
cat(x, "R lesson.")
sprintf("%s R lesson.",x)
```
## Assign variables
### Using = operator.
```{r}
x = 23
x
```
### Using <- operator.
```{r}
x <- "First program."
x
```
### Using -> operator.
```{r}
"First Program." -> x
x
```
### Multiple assignments
```{r}
x<- y<- 23
x
y
```
## Writing Basic functions.
```{r}
f_Name <- function (argument) {
statement
}
```
### Function for x power y
```{r}
power <- function(x, y) {
p <- x^y
print(paste(x,"raised to the power", y, "is", p))
}
```
### Example
```{r}
power(4,3)
```
## Working with libraries.
Libraries have multiple functions that have specific instructions to perform tasks.
### How to install packages.
```{r}
#install.packages()
```
## Plots with ggplot
```{r}
library(ggplot2)
head(iris)
```
### Barplot
```{r}
ggplot(data = iris, aes(x = Sepal.Length, y = Species)) +
geom_bar(stat= "identity")
```
### point with ggplot2.
```{r}
plot <- ggplot(data = iris, aes(x = Sepal.Length, y = Species))
plot+
geom_point()
```
### Boxplot with ggplot2
```{r}
plot+
geom_boxplot()
```
## Introduction to dplyr library
When we are working with data it is very important to know about dplyr.
In simple we can say that it makes every step easy.
### Data manupulation with dplyr
#### filter()
Filter data from any variable by choosing specific rows.
#### slice()
Choose rows based on location.
#### arrange()
Arrange the data.
#### select()
Use to select variable.
#### rename()
Use to rename variable name.
#### mutate()
Use to create a new column.
## Introduction to tidyverse package.
Tidyverse contains other 7 packages in it.
```{r}
library(tidyverse)
```
Tidyverse is actually an R package that helps with data transformations and presentation.
The whole point of tidyverse is to improve productivity and improve the application of data science using R.The purpose of tidyverse is to provide key data transformation functions in a single package. The purpose of tidyverse is to provide key data transformation functions in a single package.