直譯式語言
#這是註解
3 + 3
## [1] 6
demo(graphics)
##
##
## demo(graphics)
## ---- ~~~~~~~~
##
## > # Copyright (C) 1997-2009 The R Core Team
## >
## > require(datasets)
##
## > require(grDevices); require(graphics)
##
## > ## Here is some code which illustrates some of the differences between
## > ## R and S graphics capabilities. Note that colors are generally specified
## > ## by a character string name (taken from the X11 rgb.txt file) and that line
## > ## textures are given similarly. The parameter "bg" sets the background
## > ## parameter for the plot and there is also an "fg" parameter which sets
## > ## the foreground color.
## >
## >
## > x <- stats::rnorm(50)
##
## > opar <- par(bg = "white")
##
## > plot(x, ann = FALSE, type = "n")
##
## > abline(h = 0, col = gray(.90))
##
## > lines(x, col = "green4", lty = "dotted")
##
## > points(x, bg = "limegreen", pch = 21)
##
## > title(main = "Simple Use of Color In a Plot",
## + xlab = "Just a Whisper of a Label",
## + col.main = "blue", col.lab = gray(.8),
## + cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3)
##
## > ## A little color wheel. This code just plots equally spaced hues in
## > ## a pie chart. If you have a cheap SVGA monitor (like me) you will
## > ## probably find that numerically equispaced does not mean visually
## > ## equispaced. On my display at home, these colors tend to cluster at
## > ## the RGB primaries. On the other hand on the SGI Indy at work the
## > ## effect is near perfect.
## >
## > par(bg = "gray")
##
## > pie(rep(1,24), col = rainbow(24), radius = 0.9)
##
## > title(main = "A Sample Color Wheel", cex.main = 1.4, font.main = 3)
##
## > title(xlab = "(Use this as a test of monitor linearity)",
## + cex.lab = 0.8, font.lab = 3)
##
## > ## We have already confessed to having these. This is just showing off X11
## > ## color names (and the example (from the postscript manual) is pretty "cute".
## >
## > pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
##
## > names(pie.sales) <- c("Blueberry", "Cherry",
## + "Apple", "Boston Cream", "Other", "Vanilla Cream")
##
## > pie(pie.sales,
## + col = c("purple","violetred1","green3","cornsilk","cyan","white"))
##
## > title(main = "January Pie Sales", cex.main = 1.8, font.main = 1)
##
## > title(xlab = "(Don't try this at home kids)", cex.lab = 0.8, font.lab = 3)
##
## > ## Boxplots: I couldn't resist the capability for filling the "box".
## > ## The use of color seems like a useful addition, it focuses attention
## > ## on the central bulk of the data.
## >
## > par(bg="cornsilk")
##
## > n <- 10
##
## > g <- gl(n, 100, n*100)
##
## > x <- rnorm(n*100) + sqrt(as.numeric(g))
##
## > boxplot(split(x,g), col="lavender", notch=TRUE)
##
## > title(main="Notched Boxplots", xlab="Group", font.main=4, font.lab=1)
##
## > ## An example showing how to fill between curves.
## >
## > par(bg="white")
##
## > n <- 100
##
## > x <- c(0,cumsum(rnorm(n)))
##
## > y <- c(0,cumsum(rnorm(n)))
##
## > xx <- c(0:n, n:0)
##
## > yy <- c(x, rev(y))
##
## > plot(xx, yy, type="n", xlab="Time", ylab="Distance")
##
## > polygon(xx, yy, col="gray")
##
## > title("Distance Between Brownian Motions")
##
## > ## Colored plot margins, axis labels and titles. You do need to be
## > ## careful with these kinds of effects. It's easy to go completely
## > ## over the top and you can end up with your lunch all over the keyboard.
## > ## On the other hand, my market research clients love it.
## >
## > x <- c(0.00, 0.40, 0.86, 0.85, 0.69, 0.48, 0.54, 1.09, 1.11, 1.73, 2.05, 2.02)
##
## > par(bg="lightgray")
##
## > plot(x, type="n", axes=FALSE, ann=FALSE)
##
## > usr <- par("usr")
##
## > rect(usr[1], usr[3], usr[2], usr[4], col="cornsilk", border="black")
##
## > lines(x, col="blue")
##
## > points(x, pch=21, bg="lightcyan", cex=1.25)
##
## > axis(2, col.axis="blue", las=1)
##
## > axis(1, at=1:12, lab=month.abb, col.axis="blue")
##
## > box()
##
## > title(main= "The Level of Interest in R", font.main=4, col.main="red")
##
## > title(xlab= "1996", col.lab="red")
##
## > ## A filled histogram, showing how to change the font used for the
## > ## main title without changing the other annotation.
## >
## > par(bg="cornsilk")
##
## > x <- rnorm(1000)
##
## > hist(x, xlim=range(-4, 4, x), col="lavender", main="")
##
## > title(main="1000 Normal Random Variates", font.main=3)
##
## > ## A scatterplot matrix
## > ## The good old Iris data (yet again)
## >
## > pairs(iris[1:4], main="Edgar Anderson's Iris Data", font.main=4, pch=19)
##
## > pairs(iris[1:4], main="Edgar Anderson's Iris Data", pch=21,
## + bg = c("red", "green3", "blue")[unclass(iris$Species)])
##
## > ## Contour plotting
## > ## This produces a topographic map of one of Auckland's many volcanic "peaks".
## >
## > x <- 10*1:nrow(volcano)
##
## > y <- 10*1:ncol(volcano)
##
## > lev <- pretty(range(volcano), 10)
##
## > par(bg = "lightcyan")
##
## > pin <- par("pin")
##
## > xdelta <- diff(range(x))
##
## > ydelta <- diff(range(y))
##
## > xscale <- pin[1]/xdelta
##
## > yscale <- pin[2]/ydelta
##
## > scale <- min(xscale, yscale)
##
## > xadd <- 0.5*(pin[1]/scale - xdelta)
##
## > yadd <- 0.5*(pin[2]/scale - ydelta)
##
## > plot(numeric(0), numeric(0),
## + xlim = range(x)+c(-1,1)*xadd, ylim = range(y)+c(-1,1)*yadd,
## + type = "n", ann = FALSE)
##
## > usr <- par("usr")
##
## > rect(usr[1], usr[3], usr[2], usr[4], col="green3")
##
## > contour(x, y, volcano, levels = lev, col="yellow", lty="solid", add=TRUE)
##
## > box()
##
## > title("A Topographic Map of Maunga Whau", font= 4)
##
## > title(xlab = "Meters North", ylab = "Meters West", font= 3)
##
## > mtext("10 Meter Contour Spacing", side=3, line=0.35, outer=FALSE,
## + at = mean(par("usr")[1:2]), cex=0.7, font=3)
##
## > ## Conditioning plots
## >
## > par(bg="cornsilk")
##
## > coplot(lat ~ long | depth, data = quakes, pch = 21, bg = "green3")
##
## > par(opar)
數學運算
# 數字相加
3 + 8
## [1] 11
# 數字相減
3 - 8
## [1] -5
# 數字相乘
5 * 5
## [1] 25
# 數字相除
11 / 2
## [1] 5.5
# 指數
2^10
## [1] 1024
# 取餘數
11%%2
## [1] 1
設定變數
# 指定變數
a = 3
a
## [1] 3
# 變數相加
b = 5
c = a + b
c
## [1] 8
基礎資料型態
# 數值型態
numer = 17.8
# 字串型態
char = "Hello World"
# 布林邏輯
logic = TRUE
# 使用class 檢查資料型態
class(logic)
## [1] "logical"
不同型態資料做運算
card_length = 3
card_width = "5 inches"
#重新將card_width 指到5
card_width = 5
card_length * card_width
## [1] 15
向量 (Vector)
# 不同型態的向量
height_vec = c(180,169,173)
name_vec = c("Brian", "Toby", "Sherry")
向量的運算
x=c(1,2,3,7)
y=c(2,3,5,1)
x+y
## [1] 3 5 8 8
x*y
## [1] 2 6 15 7
x-y
## [1] -1 -1 -2 6
x/y
## [1] 0.5000 0.6667 0.6000 7.0000
將向量作加總
# 透過sum 將向量資料作加總
x = c(1,2,3,7)
sum(x)
## [1] 13
指定名稱
height_vec = c(180,169,173)
height_vec
## [1] 180 169 173
names(height_vec) = c("Brian", "Toby", "Sherry")
height_vec
## Brian Toby Sherry
## 180 169 173
name_vec = c("Brian", "Toby", "Sherry")
names(height_vec) = name_vec
判斷向量內容是否符合條件
height_vec > 175
## Brian Toby Sherry
## TRUE FALSE FALSE
height_vec < 175
## Brian Toby Sherry
## FALSE TRUE TRUE
height_vec >= 175
## Brian Toby Sherry
## TRUE FALSE FALSE
height_vec <= 175
## Brian Toby Sherry
## FALSE TRUE TRUE
height_vec == 180
## Brian Toby Sherry
## TRUE FALSE FALSE
height_vec != 180
## Brian Toby Sherry
## FALSE TRUE TRUE
自動產生向量
x = 1:20
x
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
y = seq(1,20)
y
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
seq()
seq(1,20,2)
## [1] 1 3 5 7 9 11 13 15 17 19
seq(1,3.5, by =0.5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5
seq(1,10,length=2)
## [1] 1 10
rep()
rep(c(5,8),3)
## [1] 5 8 5 8 5 8
rep(c(1,2), each=2)
## [1] 1 1 2 2
rep_len(c(1,2), 5)
## [1] 1 2 1 2 1
產生初始向量
vector("numeric", length = 10)
## [1] 0 0 0 0 0 0 0 0 0 0
vector("character", length = 10)
## [1] "" "" "" "" "" "" "" "" "" ""
vector("logical", length = 10)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
產生陣列
matrix(1:9, byrow=TRUE, nrow=3)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
matrix(1:9, nrow=3)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
建立陣列
kevin = c(85,73)
marry = c(72,64)
jerry = c(59,66)
mat = matrix(c(kevin, marry, jerry), nrow=3, byrow= TRUE)
新增欄位與列的名稱
colnames(mat) = c('first', 'second')
rownames(mat) = c('kevin', 'marry', 'jerry')
mat2 = matrix(c(kevin, marry, jerry), nrow=3, byrow=TRUE,
dimnames=list(c('kevin', 'marry', 'jerry'),c('first', 'second')))
取矩陣維度、列與欄數
dim(mat2)
## [1] 3 2
nrow(mat2)
## [1] 3
ncol(mat2)
## [1] 2
依欄或列取矩陣資料
#取第一列
mat2[1,]
## first second
## 85 73
#取第一行
mat2[,1]
## kevin marry jerry
## 85 72 59
#取第二、三列
mat2[2:3,]
## first second
## marry 72 64
## jerry 59 66
#取第二列第一行的元素
mat2[2,1]
## [1] 72
新增列與行
mat3 = rbind(mat2, c(78,63))
rownames(mat3)[nrow(mat3)] = 'sam'
mat3
## first second
## kevin 85 73
## marry 72 64
## jerry 59 66
## sam 78 63
mat4 = cbind(mat2,c(82,77,70))
colnames(mat4)[ncol(mat4)] = 'third'
mat4
## first second third
## kevin 85 73 82
## marry 72 64 77
## jerry 59 66 70
使用rowSums 及colSums
rowSums(mat2)
## kevin marry jerry
## 158 136 125
colSums(mat2)
## first second
## 216 203
矩陣運算
#矩陣宣告
m1 = matrix(1:4, byrow=TRUE, nrow=2)
m2 = matrix(5:8, byrow=TRUE, nrow=2)
#矩陣運算
m1 + m2
## [,1] [,2]
## [1,] 6 8
## [2,] 10 12
m1 - m2
## [,1] [,2]
## [1,] -4 -4
## [2,] -4 -4
m1 * m2
## [,1] [,2]
## [1,] 5 12
## [2,] 21 32
m1 / m2
## [,1] [,2]
## [1,] 0.2000 0.3333
## [2,] 0.4286 0.5000
矩陣乘積
# m1 X m2
m1 %*% m2
## [,1] [,2]
## [1,] 19 22
## [2,] 43 50
矩陣轉置
#t()
t(m1)
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
t(m2)
## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
將資料轉換為類別資料(Factor)
weather= c("sunny","rainy", "cloudy", "rainy", "cloudy")
weather_category = factor(weather)
weather_category
## [1] sunny rainy cloudy rainy cloudy
## Levels: cloudy rainy sunny
有順序的階層
temperature = c("Low", "High", "High", "Medium", "Low", "Medium")
temperature_category = factor(temperature, order = TRUE, levels = c("Low", "Medium", "High"))
temperature_category
## [1] Low High High Medium Low Medium
## Levels: Low < Medium < High
temperature_category[3] > temperature_category[1]
## [1] TRUE
temperature_category[4] > temperature_category[3]
## [1] FALSE
觀看類別
levels(weather_category)
## [1] "cloudy" "rainy" "sunny"
levels(temperature_category)
## [1] "Low" "Medium" "High"
快速轉換類別名稱
weather= c("s","r", "c", "r", "c")
weather_factor = factor(weather)
levels(weather_factor) = c("cloudy","rainy","sunny")
weather_factor
## [1] sunny rainy cloudy rainy cloudy
## Levels: cloudy rainy sunny
建立data frame
df = data.frame(a=c(1,2,3,4,5),b=c(2,3,4,5,6))
df
## a b
## 1 1 2
## 2 2 3
## 3 3 4
## 4 4 5
## 5 5 6
使用R 內建的資料集
#表列資料集
data()
#使用資料集
data(iris)
#觀察讀取到的資料集型態
class(iris)
## [1] "data.frame"
觀看資料集的前幾筆資料與後幾筆資料
#觀看前幾筆資料
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
head(iris, 10)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
#觀看後幾筆資料
tail(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica
tail(iris, 10)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 141 6.7 3.1 5.6 2.4 virginica
## 142 6.9 3.1 5.1 2.3 virginica
## 143 5.8 2.7 5.1 1.9 virginica
## 144 6.8 3.2 5.9 2.3 virginica
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica
檢視資料的架構
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
取得指定列與行的部分資料集
#取前三列資料
iris[1:3,]
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
#取前三列第一行的資料
iris[1:3,1]
## [1] 5.1 4.9 4.7
#也可以用欄位名稱取值
iris[1:3,"Sepal.Length"]
## [1] 5.1 4.9 4.7
#取前兩行資料
head(iris[,1:2])
## Sepal.Length Sepal.Width
## 1 5.1 3.5
## 2 4.9 3.0
## 3 4.7 3.2
## 4 4.6 3.1
## 5 5.0 3.6
## 6 5.4 3.9
#取特定欄位向量值
iris$"Sepal.Length"
## [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4
## [18] 5.1 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5
## [35] 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0
## [52] 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8
## [69] 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4
## [86] 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8
## [103] 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7
## [120] 6.0 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7
## [137] 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
資料篩選
#取前五筆包含length 及 width 的資料
Five.Sepal.iris = iris[1:5, c("Sepal.Length", "Sepal.Width")]
#可以用條件做篩選
setosa.data = iris[iris$Species=="setosa",1:5]
#使用which 做資料篩選
which(iris$Species=="setosa")
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
使用subset 做資料篩選
#使用subset 取欄位
Sepal.data = subset(iris, select=c("Sepal.Length", "Sepal.Width"))
#以Species 做篩選條件
setosa.data = subset(iris, Species =="setosa")
#可以做篩選條件的組合
example.data= subset(iris, Petal.Length <=1.4 & Petal.Width >= 0.2, select=Species )
資料合併
#以Merge 做資料合併
flower.type = data.frame(Species = "setosa", Flower = "iris")
merge(flower.type, iris[1:3,], by ="Species")
## Species Flower Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 setosa iris 5.1 3.5 1.4 0.2
## 2 setosa iris 4.9 3.0 1.4 0.2
## 3 setosa iris 4.7 3.2 1.3 0.2
資料排序
#用order做資料排序
iris[order(iris$Sepal.Length, decreasing = TRUE),]
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 132 7.9 3.8 6.4 2.0 virginica
## 118 7.7 3.8 6.7 2.2 virginica
## 119 7.7 2.6 6.9 2.3 virginica
## 123 7.7 2.8 6.7 2.0 virginica
## 136 7.7 3.0 6.1 2.3 virginica
## 106 7.6 3.0 6.6 2.1 virginica
## 131 7.4 2.8 6.1 1.9 virginica
## 108 7.3 2.9 6.3 1.8 virginica
## 110 7.2 3.6 6.1 2.5 virginica
## 126 7.2 3.2 6.0 1.8 virginica
## 130 7.2 3.0 5.8 1.6 virginica
## 103 7.1 3.0 5.9 2.1 virginica
## 51 7.0 3.2 4.7 1.4 versicolor
## 53 6.9 3.1 4.9 1.5 versicolor
## 121 6.9 3.2 5.7 2.3 virginica
## 140 6.9 3.1 5.4 2.1 virginica
## 142 6.9 3.1 5.1 2.3 virginica
## 77 6.8 2.8 4.8 1.4 versicolor
## 113 6.8 3.0 5.5 2.1 virginica
## 144 6.8 3.2 5.9 2.3 virginica
## 66 6.7 3.1 4.4 1.4 versicolor
## 78 6.7 3.0 5.0 1.7 versicolor
## 87 6.7 3.1 4.7 1.5 versicolor
## 109 6.7 2.5 5.8 1.8 virginica
## 125 6.7 3.3 5.7 2.1 virginica
## 141 6.7 3.1 5.6 2.4 virginica
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 59 6.6 2.9 4.6 1.3 versicolor
## 76 6.6 3.0 4.4 1.4 versicolor
## 55 6.5 2.8 4.6 1.5 versicolor
## 105 6.5 3.0 5.8 2.2 virginica
## 111 6.5 3.2 5.1 2.0 virginica
## 117 6.5 3.0 5.5 1.8 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 52 6.4 3.2 4.5 1.5 versicolor
## 75 6.4 2.9 4.3 1.3 versicolor
## 112 6.4 2.7 5.3 1.9 virginica
## 116 6.4 3.2 5.3 2.3 virginica
## 129 6.4 2.8 5.6 2.1 virginica
## 133 6.4 2.8 5.6 2.2 virginica
## 138 6.4 3.1 5.5 1.8 virginica
## 57 6.3 3.3 4.7 1.6 versicolor
## 73 6.3 2.5 4.9 1.5 versicolor
## 88 6.3 2.3 4.4 1.3 versicolor
## 101 6.3 3.3 6.0 2.5 virginica
## 104 6.3 2.9 5.6 1.8 virginica
## 124 6.3 2.7 4.9 1.8 virginica
## 134 6.3 2.8 5.1 1.5 virginica
## 137 6.3 3.4 5.6 2.4 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 69 6.2 2.2 4.5 1.5 versicolor
## 98 6.2 2.9 4.3 1.3 versicolor
## 127 6.2 2.8 4.8 1.8 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 64 6.1 2.9 4.7 1.4 versicolor
## 72 6.1 2.8 4.0 1.3 versicolor
## 74 6.1 2.8 4.7 1.2 versicolor
## 92 6.1 3.0 4.6 1.4 versicolor
## 128 6.1 3.0 4.9 1.8 virginica
## 135 6.1 2.6 5.6 1.4 virginica
## 63 6.0 2.2 4.0 1.0 versicolor
## 79 6.0 2.9 4.5 1.5 versicolor
## 84 6.0 2.7 5.1 1.6 versicolor
## 86 6.0 3.4 4.5 1.6 versicolor
## 120 6.0 2.2 5.0 1.5 virginica
## 139 6.0 3.0 4.8 1.8 virginica
## 62 5.9 3.0 4.2 1.5 versicolor
## 71 5.9 3.2 4.8 1.8 versicolor
## 150 5.9 3.0 5.1 1.8 virginica
## 15 5.8 4.0 1.2 0.2 setosa
## 68 5.8 2.7 4.1 1.0 versicolor
## 83 5.8 2.7 3.9 1.2 versicolor
## 93 5.8 2.6 4.0 1.2 versicolor
## 102 5.8 2.7 5.1 1.9 virginica
## 115 5.8 2.8 5.1 2.4 virginica
## 143 5.8 2.7 5.1 1.9 virginica
## 16 5.7 4.4 1.5 0.4 setosa
## 19 5.7 3.8 1.7 0.3 setosa
## 56 5.7 2.8 4.5 1.3 versicolor
## 80 5.7 2.6 3.5 1.0 versicolor
## 96 5.7 3.0 4.2 1.2 versicolor
## 97 5.7 2.9 4.2 1.3 versicolor
## 100 5.7 2.8 4.1 1.3 versicolor
## 114 5.7 2.5 5.0 2.0 virginica
## 65 5.6 2.9 3.6 1.3 versicolor
## 67 5.6 3.0 4.5 1.5 versicolor
## 70 5.6 2.5 3.9 1.1 versicolor
## 89 5.6 3.0 4.1 1.3 versicolor
## 95 5.6 2.7 4.2 1.3 versicolor
## 122 5.6 2.8 4.9 2.0 virginica
## 34 5.5 4.2 1.4 0.2 setosa
## 37 5.5 3.5 1.3 0.2 setosa
## 54 5.5 2.3 4.0 1.3 versicolor
## 81 5.5 2.4 3.8 1.1 versicolor
## 82 5.5 2.4 3.7 1.0 versicolor
## 90 5.5 2.5 4.0 1.3 versicolor
## 91 5.5 2.6 4.4 1.2 versicolor
## 6 5.4 3.9 1.7 0.4 setosa
## 11 5.4 3.7 1.5 0.2 setosa
## 17 5.4 3.9 1.3 0.4 setosa
## 21 5.4 3.4 1.7 0.2 setosa
## 32 5.4 3.4 1.5 0.4 setosa
## 85 5.4 3.0 4.5 1.5 versicolor
## 49 5.3 3.7 1.5 0.2 setosa
## 28 5.2 3.5 1.5 0.2 setosa
## 29 5.2 3.4 1.4 0.2 setosa
## 33 5.2 4.1 1.5 0.1 setosa
## 60 5.2 2.7 3.9 1.4 versicolor
## 1 5.1 3.5 1.4 0.2 setosa
## 18 5.1 3.5 1.4 0.3 setosa
## 20 5.1 3.8 1.5 0.3 setosa
## 22 5.1 3.7 1.5 0.4 setosa
## 24 5.1 3.3 1.7 0.5 setosa
## 40 5.1 3.4 1.5 0.2 setosa
## 45 5.1 3.8 1.9 0.4 setosa
## 47 5.1 3.8 1.6 0.2 setosa
## 99 5.1 2.5 3.0 1.1 versicolor
## 5 5.0 3.6 1.4 0.2 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 26 5.0 3.0 1.6 0.2 setosa
## 27 5.0 3.4 1.6 0.4 setosa
## 36 5.0 3.2 1.2 0.2 setosa
## 41 5.0 3.5 1.3 0.3 setosa
## 44 5.0 3.5 1.6 0.6 setosa
## 50 5.0 3.3 1.4 0.2 setosa
## 61 5.0 2.0 3.5 1.0 versicolor
## 94 5.0 2.3 3.3 1.0 versicolor
## 2 4.9 3.0 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## 35 4.9 3.1 1.5 0.2 setosa
## 38 4.9 3.6 1.4 0.1 setosa
## 58 4.9 2.4 3.3 1.0 versicolor
## 107 4.9 2.5 4.5 1.7 virginica
## 12 4.8 3.4 1.6 0.2 setosa
## 13 4.8 3.0 1.4 0.1 setosa
## 25 4.8 3.4 1.9 0.2 setosa
## 31 4.8 3.1 1.6 0.2 setosa
## 46 4.8 3.0 1.4 0.3 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 30 4.7 3.2 1.6 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 23 4.6 3.6 1.0 0.2 setosa
## 48 4.6 3.2 1.4 0.2 setosa
## 42 4.5 2.3 1.3 0.3 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 39 4.4 3.0 1.3 0.2 setosa
## 43 4.4 3.2 1.3 0.2 setosa
## 14 4.3 3.0 1.1 0.1 setosa
plyr
library(plyr)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
ddply(iris, c("Species"), function(df) mean(df$Sepal.Length))
## Species V1
## 1 setosa 5.006
## 2 versicolor 5.936
## 3 virginica 6.588
Reshape 與Aggregate
#install.packages("reshape")
library(reshape)
##
## Attaching package: 'reshape'
##
## The following objects are masked from 'package:plyr':
##
## rename, round_any
iris.melt <- melt(iris,id='Species')
cast(Species~variable,data=iris.melt,mean,
subset=Species %in% c('setosa','versicolor'),
margins='grand_row')
## Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 setosa 5.006 3.428 1.462 0.246
## 2 versicolor 5.936 2.770 4.260 1.326
## 3 (all) 5.471 3.099 2.861 0.786
aggregate(x=iris[,1:4],by=list(iris$Species),FUN=mean)
## Group.1 Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 setosa 5.006 3.428 1.462 0.246
## 2 versicolor 5.936 2.770 4.260 1.326
## 3 virginica 6.588 2.974 5.552 2.026
清單(Lists)
item = list(thing="hat", size=8.25)
item
## $thing
## [1] "hat"
##
## $size
## [1] 8.25
test = list(name="Toby", score = c(87,57,72))
test$score
## [1] 87 57 72
test$score[2]
## [1] 57
清單(Lists)
flower= list(title="iris dataset", data=iris)
str(flower)
## List of 2
## $ title: chr "iris dataset"
## $ data :'data.frame': 150 obs. of 5 variables:
## ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
flower$data[1,"Sepal.Width"]
## [1] 3.5
讀取資料
#read.table
test.data = read.table(header = TRUE, text = "
a b
1 2
3 4
")
#觀看資料屬性
class(test.data)
## [1] "data.frame"
寫入資料
#table
write.table(test.data, file = "test.txt" , sep = " ")
#csv
write.csv(test.data, file = "test.csv")
IF…ELSE…
x = 5;
if(x > 3){
print("x > 3");
}else{
print("x <= 3");
}
## [1] "x > 3"
IF…ELSE IF…ELSE
x = 5;
if(x > 3){
print("x > 3");
} else if(x ==3){
print("x == 3");
}else{
print("x < 3");
}
## [1] "x > 3"
FOR 迴圈
#For 迴圈
for(i in 1:10){
print (i);
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
#1~100的總和
sum = 0
for(i in 1:100){
sum = sum + i;
}
sum
## [1] 5050
三種FOR 迴圈
x <- c("sunny","rainy", "cloudy", "rainy", "cloudy")
for(i in 1:length(x)) {
print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
for(i in seq_along(x)) {
print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
for(letter in x) {
print(letter)
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
兩層for 迴圈
mat = matrix(1:9, byrow=TRUE, nrow=3)
for(i in seq_len(nrow(mat))) {
for(j in seq_len(ncol(mat))) {
print(mat[i, j])
}
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
使用while 迴圈
sum = 0;
cnt = 0;
while(cnt <= 100){
sum = sum + cnt;
cnt = cnt + 1;
}
sum
## [1] 5050
使用Repeat
sum = 0;
cnt = 0;
repeat{
cnt = cnt + 1;
if(cnt > 100) break;
sum = sum + cnt;
}
sum;
## [1] 5050
Lazy Function
f = function(a, b) {
a * 2
}
f(3)
## [1] 6
f = function(a, b) {
print(a+ b)
}
#f(3)