Wush Wu
Taiwan R User Group
| 資料衡量尺度 | 變數形態 | 特性 | |
|---|---|---|---|
| 1 | 名目資料 | 質化 | 類別 |
| 2 | 順序資料 | 質化 | 優先順序 |
| 3 | 區間資料 | 量化 | 大小距離 |
| 4 | 比例資料 | 量化 | 比值 |
數值只用於記號,值毫無意義的數據
| 男生 | 女生 |
|---|---|
| 0 | 1 |
| 男生 | 女生 |
|---|---|
| 1 | 2 |
數值有順序關係,但是差距沒有意義
有差距的概念,沒有倍數的概念。 數值有1的概念,沒有0的概念。可加減。
同時有差距和倍數的概念。 數值有1和0的概念。可加減乘除。
#R,Text Mining Series,Taiwan R User Group| 資料衡量尺度 | 變數形態 | 特性 | |
|---|---|---|---|
| 1 | 名目資料 | 質化 | 類別 |
| 2 | 順序資料 | 質化 | 優先順序 |
| 3 | 區間資料 | 量化 | 大小距離 |
| 4 | 比例資料 | 量化 | 比值 |
| 同質形 | 異質形 |
|---|---|
| Atomic Type | List |
| Matrix | Data Frame |
| Array |
今天說好不提的...
所有的資料都是透過Atomic Type組合而成

"或'來包覆要輸入的文字
\"來輸入"或\' plot(cars, main = "\"hello world\"")
pastex <- "abc"
y <- "bbb"
paste(x, y, sep = ",")
## [1] "abc,bbb"
strsplitstrsplit(x, "b")
## [[1]]
## [1] "a" "c"
ncharnchar(x)
## [1] 3
substringsubstring(x, 1, 2)
## [1] "ab"
T、TRUE、F或FALSE輸入x <- 1
x < 2
## [1] TRUE
x <= 1
## [1] TRUE
if (T) {
print("This is TRUE")
} else {
print("This is FALSE")
}
## [1] "This is TRUE"
!TRUE
## [1] FALSE
T & T
## [1] TRUE
T | F
## [1] TRUE
沒什麼特別的就跳過吧!
+1 + 2
## [1] 3
-1 - 2
## [1] -1
*1 * 2
## [1] 2
/1L/2L
## [1] 0.5
length(0L)
## [1] 1
dim就變成矩陣、陣列x <- 1L
dim(x) <- c(1, 1)
x
## [,1]
## [1,] 1
:或c()快速建立Vector1:3
## [1] 1 2 3
c(1L, 2L, 3L)
## [1] 1 2 3
1:3 + 2:4
## [1] 3 5 7
1:2 + 1:3
## Warning: 較長的物件長度並非較短物件長度的倍數
## [1] 2 4 4
1:2 + 1:3
## Warning: 較長的物件長度並非較短物件長度的倍數
## [1] 2 4 4
1 2 1 2
1 2 3
2 4 4
f1 <- function() 1:1000 + 1
f2 <- function() {
r <- integer(1000)
for (i in 1:1000) r[i] <- i + 1
r
}
| expr | median(nano seconds) | |
|---|---|---|
| 1 | f2() | 1215099.00 |
| 2 | f1() | 3589.50 |
x <- c(1L, 2, "3")
class(x)
## [1] "character"
x
## [1] "1" "2" "3"
x <- matrix(1:4, 2, 2)
class(x)
## [1] "matrix"
x[2]
## [1] 2
x[2] <- as.character(x[2])
x
## [,1] [,2]
## [1,] "1" "3"
## [2,] "2" "4"
x <- list(1L, 2, "3")
x
## [[1]]
## [1] 1
##
## [[2]]
## [1] 2
##
## [[3]]
## [1] "3"
x <- list(1L, 2, "3", mean)
x[[4]]
## function (x, ...)
## UseMethod("mean")
## <bytecode: 0x1067098e0>
## <environment: namespace:base>
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.10 | 3.50 | 1.40 | 0.20 | setosa |
| 4.90 | 3.00 | 1.40 | 0.20 | setosa |
| 4.70 | 3.20 | 1.30 | 0.20 | setosa |
| 4.60 | 3.10 | 1.50 | 0.20 | setosa |
| 5.00 | 3.60 | 1.40 | 0.20 | setosa |
| 5.40 | 3.90 | 1.70 | 0.40 | setosa |
length(iris$Sepal.Length)
## [1] 150
length(iris$Species)
## [1] 150
class(iris)
## [1] "data.frame"
is.list(iris)
## [1] TRUE
data.frame(a = 1L, b = "2")
## a b
## 1 1 2
asas.characteras.logicalas.integeras.numericas.numeric("2")
## [1] 2
as.integer("a")
## Warning: 強制變更過程中產生了 NA
## [1] NA
注意用Operator處理NA,通常會噴NA
z <- c(1:3, NA)
is.na(z)
## [1] FALSE FALSE FALSE TRUE
z == NA
## [1] NA NA NA NA
常用於處理質性變數
x <- c(2, 1, 2, 2)
x
## [1] 2 1 2 2
x <- factor(c(2, 1, 2, 2), levels = 2:1)
x
## [1] 2 1 2 2
## Levels: 2 1
levels(x)
## [1] "2" "1"
這些integer其實代表index
x <- factor(c(2, 1, 2, 2), levels = 2:1)
as.integer(x)
## [1] 1 2 1 1
levels(x)
## [1] "2" "1"
levels(x)[as.integer(x)]
## [1] "2" "1" "2" "2"
format、as.POSIXct和字串轉換as.POSIXct("2014-01-01 23:50:34")
## [1] "2014-01-01 23:50:34 CST"
format(Sys.time())
## [1] "2014-02-24 10:37:15"
format(Sys.time(), "%Y%m%d %H%M%S")
## [1] "20140224 103715"
as.integer(Sys.time())
## [1] 1393209435
x <- as.POSIXct("1-01-01 00:00:00")
as.integer(x)
## Warning: 強制變更過程中產生了 NA
## [1] NA
眾數、中位數、四分位差和算數平均數能用於哪些資料形態
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.30 Min. :2.00 Min. :1.00 Min. :0.1
## 1st Qu.:5.10 1st Qu.:2.80 1st Qu.:1.60 1st Qu.:0.3
## Median :5.80 Median :3.00 Median :4.35 Median :1.3
## Mean :5.84 Mean :3.06 Mean :3.76 Mean :1.2
## 3rd Qu.:6.40 3rd Qu.:3.30 3rd Qu.:5.10 3rd Qu.:1.8
## Max. :7.90 Max. :4.40 Max. :6.90 Max. :2.5
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
age <- c("18", "17", "10")
name <- c("Wush", "c3h3", "+1")
tool <- c("R", "R,Python", "Python")
df <- data.frame(age = age, name = name, tool = tool)
df
## age name tool
## 1 18 Wush R
## 2 17 c3h3 R,Python
## 3 10 +1 Python
[[ + logicalx <- 1:10
x
## [1] 1 2 3 4 5 6 7 8 9 10
head(1:10 < 3)
## [1] TRUE TRUE FALSE FALSE FALSE FALSE
x[1:10 < 3]
## [1] 1 2
[ + integer/numericx[1:3]
## [1] 1 2 3
[ + characternames(x) <- letters[1:10]
x
## a b c d e f g h i j
## 1 2 3 4 5 6 7 8 9 10
x[c("a", "c")]
## a c
## 1 3
x <- matrix(1:4, 2, 2)
x
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
x[1, 2]
## [1] 3
x[1:2, 1]
## [1] 1 2
x <- array(1:8, c(2, 2, 2))
x
## , , 1
##
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## , , 2
##
## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
x[1, 2, 1]
## [1] 3
x <- matrix(1:4, 2, 2)
2
## [1] 2
x[1:2, 1]
## [1] 1 2
x[1:2, 1, drop = FALSE]
## [,1]
## [1,] 1
## [2,] 2
iris[1:2, 1:2]
## Sepal.Length Sepal.Width
## 1 5.1 3.5
## 2 4.9 3.0
head(iris[, 3])
## [1] 1.4 1.4 1.3 1.5 1.4 1.7
(x <- list(a = 1L, b = "2"))
## $a
## [1] 1
##
## $b
## [1] "2"
x[2]
## $b
## [1] "2"
x[[2]]
## [1] "2"
x[["a"]]
## [1] 1
x$a
## [1] 1
[<-(x <- matrix(1:4, 2, 2))
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
(x[1, 2] <- 100)
## [1] 100
x
## [,1] [,2]
## [1,] 1 100
## [2,] 2 4
(x <- list(a = 1, b = "2"))
## $a
## [1] 1
##
## $b
## [1] "2"
x$b <- pi
x
## $a
## [1] 1
##
## $b
## [1] 3.142
c(1:4, 5)
## [1] 1 2 3 4 5
x <- 1:4
x[5] <- 5
x
## [1] 1 2 3 4 5
length(x) <- 6
x
## [1] 1 2 3 4 5 NA
x <- 1:4
length(x) <- 3
x
## [1] 1 2 3
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
setosa而且Petal.Length不超過1.5的資料Species和Sepal.LengthSepal.Length標準化(平均值調整為0, 變異數調整為1)Sepal.Length排序(?order)Sepal.Length的平均age <- c("18", "17", "10")
name <- c("Wush", "c3h3", "+1")
tool <- c("R", "R,Python", "Python")
df <- data.frame(age = age, name = name, tool = tool)
df
## age name tool
## 1 18 Wush R
## 2 17 c3h3 R,Python
## 3 10 +1 Python
http://blog.rstudio.org/2014/01/17/introducing-dplyr/
plyrRcpp的大大Romain Francois)library(data.table);?data.table)請選出Species是setosa而且Petal.Length不超過1.5的資料
head(filter(iris, Species == "setosa", Petal.Length <= 1.5))
## 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 4.6 3.4 1.4 0.3 setosa
請挑出Species和Sepal.Length
head(select(iris, Species, Sepal.Length))
## Species Sepal.Length
## 1 setosa 5.1
## 2 setosa 4.9
## 3 setosa 4.7
## 4 setosa 4.6
## 5 setosa 5.0
## 6 setosa 5.4
請把Sepal.Length標準化(平均值調整為0, 變異數調整為1)
head(mutate(iris, zSepal.Length = scale(Sepal.Length)))
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species zSepal.Length
## 1 5.1 3.5 1.4 0.2 setosa -0.8977
## 2 4.9 3.0 1.4 0.2 setosa -1.1392
## 3 4.7 3.2 1.3 0.2 setosa -1.3807
## 4 4.6 3.1 1.5 0.2 setosa -1.5015
## 5 5.0 3.6 1.4 0.2 setosa -1.0184
## 6 5.4 3.9 1.7 0.4 setosa -0.5354
請把資料依照Sepal.Length排序(?order)
head(arrange(iris, Sepal.Length), 3)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 4.3 3.0 1.1 0.1 setosa
## 2 4.4 2.9 1.4 0.2 setosa
## 3 4.4 3.0 1.3 0.2 setosa
請把資料依照Sepal.Length, Petal.Length排序(?order)
head(arrange(iris, Sepal.Length, Petal.Length), 3)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 4.3 3.0 1.1 0.1 setosa
## 2 4.4 3.0 1.3 0.2 setosa
## 3 4.4 3.2 1.3 0.2 setosa
請計算Sepal.Length的平均
head(summarise(iris, mean(Sepal.Length)))
## mean(Sepal.Length)
## 1 5.843
請計算各Species在iris資料中出現的個數
iris.g <- group_by(iris, Species)
head(iris.g, 2)
## Source: local data frame [2 x 5]
## Groups: Species
##
## 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
summarise(iris.g, length(Species))
## Source: local data frame [3 x 2]
##
## Species length(Species)
## 1 virginica 50
## 2 versicolor 50
## 3 setosa 50