-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTutorial1_introR.Rmd
More file actions
109 lines (90 loc) · 3.07 KB
/
Tutorial1_introR.Rmd
File metadata and controls
109 lines (90 loc) · 3.07 KB
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
---
title: "Introduction to R"
output:
html_document: default
html_notebook: default
pdf_document: default
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Executing each chunk by clicking the *Run* button within the chunk.
```{r, echo=TRUE}
1+2
3*5
pi
3.5^3
sqrt(9)
```
```{r, echo=TRUE}
height<-c(64, 67, 68, 70, 65)
weight<-c(120, 140, 165, 190, 130)
summary(height)
weight
height
dat1=cbind(weight, height)
dat1
```
```{r}
dat1=as.data.frame(dat1)
plot(dat1$height, dat1$weight, pch=2)
plot(weight~height, data=dat1, pch=16)
```
#### Plotting symbols and colors
```{r}
1:20
1:3
1:20+1:3
plot(1:20, 1:20, pch=1:20, col=1:6)
```
`R` has a *recycling rule*: "Vectors occurring in the same expression need not all be of the same length. If they are not, the value of the expression is a vector with the same length as the longest vector which occurs in the expression. Shorter vectors in the expression are recycled as often as need be (perhaps fractionally) until they match the length of the longest vector. In particular a constant is simply repeated."
In the above example, I only specified 6 colors where there are 20 points. The six colors are simply repeated.
In `R` colors can be called by names as well. See [here](http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf) for list of colors and their names
```{r, fig.width=4, fig.height=6}
plot(rnorm(30, 0, 1), rnorm(30, 0, 1), pch=8, cex=2, col=c("dodgerblue1", "firebrick1"))
```
`RColorBrewer` is R package that allow automatic generations of colors. It can generate colors from different [palettes](https://i1.wp.com/bc.bojanorama.pl/wp-content/uploads/2013/04/rcolorsheet-1.png).
```{r, fig.width=4, fig.height=5}
library(RColorBrewer)
col.use=brewer.pal(5, "RdYlBu")
plot(runif(50, 0, 1), runif(50, 0, 1), pch=c(8, 18), cex=2, col=col.use)
```
#### Equally spaced values
```{r}
1:20 # Integers from 1 to 20.
-4:5 # Integers from -4 to 5
5:1 # Integers from 5 to 1
seq(0,1, len=21) # 20 numbers equally spaced between 0 and 1.
```
#### Repeated values
```{r}
rep(c(1,2,3), each=3)
rep(c(1,2,3), c(2, 5, 4))
rep(c(2,4,6), 4)
```
#### Enter data using `c()`.
R can read data from the console. This is not practical for real data sets.
```{r}
x <- c(1, 23, 2, 3, 12, 18, 6, 9)
x
mean(x)
median(x)
var(x)
sd(x)
par(mfrow=c(1,2))
boxplot(x)
hist(x)
```
## Now, your challenge
+ Step 1: Add a new chunk by clicking the *Insert Chunk* button on the toolbar.
```{r}
x=seq(-pi, pi, len=100)
y=sin(x)
plot(x, y, col="purple", type="h")
curve(sin(x), -pi, pi)
```
+ Step 2: Make a vector x that consists of 30 equally spaced values between -1 and 1.
+ Step 3: Make a vector `y=sin(x)`
+ Step 4: Make a scatter plot of y and x.
+ Step 5: Change the plotting character to a diamond.
+ Step 6: Change the color of your dots.
+ Step 7: Read `help(plot)` about different types of plot specified by `type =`.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button to preview the HTML file).