This is an R Markdown 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.
1+2
## [1] 3
3*5
## [1] 15
pi
## [1] 3.141593
3.5^3
## [1] 42.875
sqrt(9)
## [1] 3
height<-c(64, 67, 68, 70, 65)
weight<-c(120, 140, 165, 190, 130)
summary(height)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 64.0 65.0 67.0 66.8 68.0 70.0
weight
## [1] 120 140 165 190 130
height
## [1] 64 67 68 70 65
dat1=cbind(weight, height)
dat1
## weight height
## [1,] 120 64
## [2,] 140 67
## [3,] 165 68
## [4,] 190 70
## [5,] 130 65
dat1=as.data.frame(dat1)
plot(dat1$height, dat1$weight, pch=2)
plot(weight~height, data=dat1, pch=16)
1:20
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1:3
## [1] 1 2 3
1:20+1:3
## Warning in 1:20 + 1:3: longer object length is not a multiple of shorter
## object length
## [1] 2 4 6 5 7 9 8 10 12 11 13 15 14 16 18 17 19 21 20 22
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 for list of colors and their names
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.
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)
1:20 # Integers from 1 to 20.
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
-4:5 # Integers from -4 to 5
## [1] -4 -3 -2 -1 0 1 2 3 4 5
5:1 # Integers from 5 to 1
## [1] 5 4 3 2 1
seq(0,1, len=21) # 20 numbers equally spaced between 0 and 1.
## [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65
## [15] 0.70 0.75 0.80 0.85 0.90 0.95 1.00
rep(c(1,2,3), each=3)
## [1] 1 1 1 2 2 2 3 3 3
rep(c(1,2,3), c(2, 5, 4))
## [1] 1 1 2 2 2 2 2 3 3 3 3
rep(c(2,4,6), 4)
## [1] 2 4 6 2 4 6 2 4 6 2 4 6
c().R can read data from the console. This is not practical for real data sets.
x <- c(1, 23, 2, 3, 12, 18, 6, 9)
x
## [1] 1 23 2 3 12 18 6 9
mean(x)
## [1] 9.25
median(x)
## [1] 7.5
var(x)
## [1] 63.35714
sd(x)
## [1] 7.95972
par(mfrow=c(1,2))
boxplot(x)
hist(x)
x=seq(-pi, pi, len=100)
y=sin(x)
plot(x, y, col="purple", type="h")
curve(sin(x), -pi, pi)
y=sin(x)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).