what is R? - https://zh.wikipedia.org/wiki/R%E8%AF%AD%E8%A8%80
R vs Python? - https://www.datacamp.com/community/tutorials/r-or-python-for-data-analysis
most popular programming language for analytics - http://www.kdnuggets.com/polls/2015/r-vs-python.html - http://www.kdnuggets.com/2016/06/r-python-top-analytics-data-mining-data-science-software.html
Kaggle - https://www.kaggle.com/
UCI dataset - https://archive.ics.uci.edu/ml/datasets.html
#使用範例資料
data(anscombe)
#使用資料中x1,y1變數畫出點散布圖
plot(y1 ~ x1, data = anscombe)
#建立回歸模型並assign到lmfit變數中
lmfit <- lm(y1~x1, data=anscombe)
#在點散佈圖上加上迴歸線
abline(lmfit, col="red")
#文件查詢
help(package="base")
?base::sum
?sum
help.search("sum")
??sum
#範例演釋
demo()
#使用內建資料集
data()
#看現有變數
ls()
## [1] "anscombe" "lmfit"
#移除變數
rm()
x = c(1,2,3)
#查看資料型態
class(x)
## [1] "numeric"
#查看資料結構
str(x)
## num [1:3] 1 2 3
3+8
## [1] 11
3-8
## [1] -5
3*8
## [1] 24
3/8
## [1] 0.375
1/0
## [1] Inf
11%%2
## [1] 1
3<4
## [1] TRUE
2==5
## [1] FALSE
T == TRUE
## [1] TRUE
a = 3
a <- 3 # ( alt + - )
assign("a",3)
a / 2
## [1] 1.5
a = a / 2
a
## [1] 1.5
character(5) ## character vector of length 5
## [1] "" "" "" "" ""
numeric(5)
## [1] 0 0 0 0 0
logical(5)
## [1] FALSE FALSE FALSE FALSE FALSE
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.5000000 0.6666667 0.6000000 7.0000000
x = c(1,2,3,7)
x + 10
## [1] 11 12 13 17
x + c(10)
## [1] 11 12 13 17
x + c(1,2)
## [1] 2 4 4 9
x + c(1,2,1,2)
## [1] 2 4 4 9
x == c(1,99,3,4)
## [1] TRUE FALSE TRUE FALSE
c(1,2,3)
## [1] 1 2 3
c(2,T,3+0i,"one")
## [1] "2" "TRUE" "3+0i" "one"
c(2,T,3+0i)
## [1] 2+0i 1+0i 3+0i
c(c(1,2,3,4),c(5))
## [1] 1 2 3 4 5
x = c(1,2,3,4,NA)
is.na(x)
## [1] FALSE FALSE FALSE FALSE TRUE
sum(x)
## [1] NA
sum(x, na.rm=T)
## [1] 10
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 / 100
## Brian Toby Sherry
## 1.80 1.69 1.73
height_vec > 175 | height_vec < 170
## Brian Toby Sherry
## TRUE TRUE FALSE
height_vec < 175 & height_vec > 170
## Brian Toby Sherry
## FALSE FALSE TRUE
#R 的index從1開始
height_vec[c(1)] #index
## Brian
## 180
height_vec['Brian'] #element name
## Brian
## 180
height_vec[height_vec > 175] #condition (boolean vector)
## Brian
## 180
1:20
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
seq(1,20)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
20:1
## [1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
?seq
seq(from=1,to=20,by=2)
## [1] 1 3 5 7 9 11 13 15 17 19
seq(from=1,to=20,length=2)
## [1] 1 20
rep(1,5)
## [1] 1 1 1 1 1
?rep
rep(x=c(1,2), times=5)
## [1] 1 2 1 2 1 2 1 2 1 2
rep(x=c(1,2), times=c(1,2))
## [1] 1 2 2
rep(x=c(1,2), each=5)
## [1] 1 1 1 1 1 2 2 2 2 2
rep_len(x=c(1,2),length.out = 5)
## [1] 1 2 1 2 1
paste("the","big","bang","theory")
## [1] "the big bang theory"
paste("big","bang",sep="-")
## [1] "big-bang"
length(paste("the","big","bang","theory"))
## [1] 1
paste("big","bang",sep="")
## [1] "bigbang"
paste("big","bang",sep=";")
## [1] "big;bang"
paste(c("big","bang"),1:2)
## [1] "big 1" "bang 2"
paste(c("big","bang"),1:2,collapse = "+" )
## [1] "big 1+bang 2"
length(paste(c("big","bang"),1:4,collapse = "+" ))
## [1] 1
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')
mat
## first second
## kevin 85 73
## marry 72 64
## jerry 59 66
#取得矩陣維度
dim(mat)
## [1] 3 2
#取得矩陣列數
nrow(mat)
## [1] 3
#取得矩陣欄數
ncol(mat)
## [1] 2
#矩陣轉置(transpose)
t(mat)
## kevin marry jerry
## first 85 72 59
## second 73 64 66
#取第一列
mat[1,]
## first second
## 85 73
#取第一行
mat[,1]
## kevin marry jerry
## 85 72 59
#取第一、二列
mat[1:2,]
## first second
## kevin 85 73
## marry 72 64
#取kevin和jerry成績
mat[c('kevin','jerry'),]
## first second
## kevin 85 73
## jerry 59 66
#取kevin和jerry成績的第一次考試成績
mat[c('kevin','jerry'),'first']
## kevin jerry
## 85 59
#取得第一次考試成績不及格的人
mat[mat[,1] < 60,'first']
## [1] 59
rowSums(mat)
## kevin marry jerry
## 158 136 125
colSums(mat)
## first second
## 216 203
rowMeans(mat)
## kevin marry jerry
## 79.0 68.0 62.5
colMeans(mat)
## first second
## 72.00000 67.66667
#新增列與行
mat2 = rbind(mat, c(78,63))
rownames(mat2)[nrow(mat2)] = 'sam'
mat2
## first second
## kevin 85 73
## marry 72 64
## jerry 59 66
## sam 78 63
mat3 = cbind(mat2,c(82,77,70,64))
colnames(mat3)[ncol(mat3)] = 'third'
mat3
## first second third
## kevin 85 73 82
## marry 72 64 77
## jerry 59 66 70
## sam 78 63 64
rowMeans(mat3)
## kevin marry jerry sam
## 80.00000 71.00000 65.00000 68.33333
colMeans(mat3)
## first second third
## 73.50 66.50 73.25
# arithmetic
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.2000000 0.3333333
## [2,] 0.4285714 0.5000000
m1 %*% m2
## [,1] [,2]
## [1,] 19 22
## [2,] 43 50
# syntax
weather= c("sunny","rainy", "cloudy", "rainy", "cloudy")
weather_category = factor(weather)
weather_category
## [1] sunny rainy cloudy rainy cloudy
## Levels: cloudy rainy sunny
class(weather)
## [1] "character"
class(weather_category)
## [1] "factor"
# order
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
# change levels name
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
name <- c("Joe", "Bob", "Vicky")
age <- c(28, 26, 34)
gender <- c("Male","Male","Female")
df <- data.frame(name, age, gender)
class(df)
## [1] "data.frame"
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ name : Factor w/ 3 levels "Bob","Joe","Vicky": 2 1 3
## $ age : num 28 26 34
## $ gender: Factor w/ 2 levels "Female","Male": 2 2 1
data(iris)
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
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
#取前三列Sepal.Length欄位的資料
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:3]
## [1] 5.1 4.9 4.7
#取前五筆包含length 及 width 的資料
Five.Sepal.iris = iris[1:5, c("Sepal.Length","Sepal.Width")]
#可以用條件做篩選
setosa.data = iris[iris$Species=="setosa",1:5]
str(setosa.data)
## 'data.frame': 50 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 ...
#使用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
#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
df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
#Inner join:
merge(x = df1, y= df2, by="CustomerId")
## CustomerId Product State
## 1 2 Toaster Alabama
## 2 4 Radio Alabama
## 3 6 Radio Ohio
#Outer join:
merge(x = df1, y = df2, by = "CustomerId", all = TRUE)
## CustomerId Product State
## 1 1 Toaster <NA>
## 2 2 Toaster Alabama
## 3 3 Toaster <NA>
## 4 4 Radio Alabama
## 5 5 Radio <NA>
## 6 6 Radio Ohio
#Left outer:
merge(x = df1, y = df2, by = "CustomerId", all.x = TRUE)
## CustomerId Product State
## 1 1 Toaster <NA>
## 2 2 Toaster Alabama
## 3 3 Toaster <NA>
## 4 4 Radio Alabama
## 5 5 Radio <NA>
## 6 6 Radio Ohio
#Right outer:
merge(x = df1, y = df2, by = "CustomerId", all.y = TRUE)
## CustomerId Product State
## 1 2 Toaster Alabama
## 2 4 Radio Alabama
## 3 6 Radio Ohio
#Cross join:
merge(x = df1, y = df2, by = NULL)
## CustomerId.x Product CustomerId.y State
## 1 1 Toaster 2 Alabama
## 2 2 Toaster 2 Alabama
## 3 3 Toaster 2 Alabama
## 4 4 Radio 2 Alabama
## 5 5 Radio 2 Alabama
## 6 6 Radio 2 Alabama
## 7 1 Toaster 4 Alabama
## 8 2 Toaster 4 Alabama
## 9 3 Toaster 4 Alabama
## 10 4 Radio 4 Alabama
## 11 5 Radio 4 Alabama
## 12 6 Radio 4 Alabama
## 13 1 Toaster 6 Ohio
## 14 2 Toaster 6 Ohio
## 15 3 Toaster 6 Ohio
## 16 4 Radio 6 Ohio
## 17 5 Radio 6 Ohio
## 18 6 Radio 6 Ohio
#用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
sort(iris$Sepal.Length, decreasing = TRUE)
## [1] 7.9 7.7 7.7 7.7 7.7 7.6 7.4 7.3 7.2 7.2 7.2 7.1 7.0 6.9 6.9 6.9 6.9
## [18] 6.8 6.8 6.8 6.7 6.7 6.7 6.7 6.7 6.7 6.7 6.7 6.6 6.6 6.5 6.5 6.5 6.5
## [35] 6.5 6.4 6.4 6.4 6.4 6.4 6.4 6.4 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3
## [52] 6.2 6.2 6.2 6.2 6.1 6.1 6.1 6.1 6.1 6.1 6.0 6.0 6.0 6.0 6.0 6.0 5.9
## [69] 5.9 5.9 5.8 5.8 5.8 5.8 5.8 5.8 5.8 5.7 5.7 5.7 5.7 5.7 5.7 5.7 5.7
## [86] 5.6 5.6 5.6 5.6 5.6 5.6 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.4 5.4 5.4 5.4
## [103] 5.4 5.4 5.3 5.2 5.2 5.2 5.2 5.1 5.1 5.1 5.1 5.1 5.1 5.1 5.1 5.1 5.0
## [120] 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 4.9 4.9 4.9 4.9 4.9 4.9 4.8 4.8
## [137] 4.8 4.8 4.8 4.7 4.7 4.6 4.6 4.6 4.6 4.5 4.4 4.4 4.4 4.3
#資料轉換
iris$Species_new = ifelse(iris$Species == c("setosa"), "IsSetosa","Notsetosa")
iris$Species_new
## [1] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [6] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [11] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [16] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [21] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [26] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [31] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [36] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [41] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [46] "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa" "IsSetosa"
## [51] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [56] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [61] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [66] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [71] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [76] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [81] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [86] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [91] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [96] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [101] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [106] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [111] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [116] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [121] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [126] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [131] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [136] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [141] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
## [146] "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa" "Notsetosa"
# getwd()
setwd('~/lecture/riii')
# setwd("__your_working_directory_path__")
tw2330 = read.csv("data/2330.csv", header=TRUE)
test.data = read.table("data/match.txt" ,header = FALSE, sep="|")
#table
write.table(test.data, file = "data/test.txt" , sep = " ")
#csv
write.csv(test.data, file = "data/test.csv")
item= list(thing='hat',size=8.25)
item$thing
## [1] "hat"
item$size
## [1] 8.25