transalte 74%

This commit is contained in:
alswl 2013-09-18 21:51:48 +08:00
parent 6b9ac70e5e
commit 3ff90ab9eb

View File

@ -299,42 +299,32 @@ jiggle(5) # 5±ε. 使用 set.seed(2716057) 后, jiggle(5)==5.005043
######################### #########################
# 单维度 # 单维度
# You can vectorize anything, so long as all components have the same type # 你可以将目前我们学习到的任何类型矢量化,只要它们拥有相同的类型
#你可以将任何东西矢量化,因此所有的组分都有相同的类型
vec <- c(8, 9, 10, 11) vec <- c(8, 9, 10, 11)
vec # 8 9 10 11 vec # 8 9 10 11
# The class of a vector is the class of its components # 矢量的类型是这一组数据元素的类型
#矢量class表示这一组分的类型
class(vec) # "numeric" class(vec) # "numeric"
# If you vectorize items of different classes, weird coercions happen # If you vectorize items of different classes, weird coercions happen
#如果你强制的将不同类型的classes矢量化会发生超自然形式的函数例如都转变成数值型、字符型 #如果你强制的将不同类型数值矢量化,会出现特殊值
c(TRUE, 4) # 1 4 c(TRUE, 4) # 1 4
c("dog", TRUE, 4) # "dog" "TRUE" "4" c("dog", TRUE, 4) # "dog" "TRUE" "4"
# We ask for specific components like so (R starts counting from 1) #我们这样来取内部数据R 的下标索引顺序 1 开始)
#我们可以找寻特定的组分例如这个例子R从1算起
vec[1] # 8 vec[1] # 8
# We can also search for the indices of specific components, # 我们可以根据条件查找特定数据
#我们也可以从这些特定组分中找寻这些指标
which(vec %% 2 == 0) # 1 3 which(vec %% 2 == 0) # 1 3
# or grab just the first or last entry in the vector # 抓取矢量中第一个和最后一个字符
#抓取矢量中第1个和最后一个字符
head(vec, 1) # 8 head(vec, 1) # 8
tail(vec, 1) # 11 tail(vec, 1) # 11
#如果指数结束或不存在即"goes over" 可以获得NA #如果下标溢出或不存会得到 NA
# If an index "goes over" you'll get NA:
vec[6] # NA vec[6] # NA
# You can find the length of your vector with length() # 你可以使用 length() 获取矢量的长度
#你也可以找到矢量的长度
length(vec) # 4 length(vec) # 4
# You can perform operations on entire vectors or subsets of vectors # 你可以直接操作矢量或者矢量的子集
#你可以将整个矢量或者子矢量集进行展示
vec * 4 # 16 20 24 28 vec * 4 # 16 20 24 28
#
vec[2:3] * 5 # 25 30 vec[2:3] * 5 # 25 30
# and there are many built-in functions to summarize vectors # 这里有许多内置的函数,来表现向量
#这里有许多内置的功能函数,并且可对矢量特征进行总结
mean(vec) # 9.5 mean(vec) # 9.5
var(vec) # 1.666667 var(vec) # 1.666667
sd(vec) # 1.290994 sd(vec) # 1.290994
@ -342,40 +332,32 @@ max(vec) # 11
min(vec) # 8 min(vec) # 8
sum(vec) # 38 sum(vec) # 38
# TWO-DIMENSIONAL (ALL ONE CLASS) # 二维(相同元素类型)
#二维函数
# You can make a matrix out of entries all of the same type like so: #你可以为同样类型的变量建立矩阵
#你可以建立矩阵,保证所有的变量形式相同
mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6)) mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6))
#建立mat矩阵3行2列从1到6排列默认按列排布
mat mat
# => # =>
# [,1] [,2] # [,1] [,2]
# [1,] 1 4 # [1,] 1 4
# [2,] 2 5 # [2,] 2 5
# [3,] 3 6 # [3,] 3 6
# Unlike a vector, the class of a matrix is "matrix", no matter what's in it # 和 vector 不一样的是,一个矩阵的类型真的是 「matrix」而不是内部元素的类型
class(mat) # => "matrix" class(mat) # => "matrix"
# Ask for the first row
# 访问第一行的字符 # 访问第一行的字符
mat[1,] # 1 4 mat[1,] # 1 4
# Perform operation on the first column # 操作第一行数据
#优先输入第一列分别×3输出
3 * mat[,1] # 3 6 9 3 * mat[,1] # 3 6 9
# Ask for a specific cell # 访问一个特定数据
#访问特殊的单元第3行第二列
mat[3,2] # 6 mat[3,2] # 6
# Transpose the whole matrix # 转置整个矩阵(译者注:变成 2 行 3 列)
#转置整个矩阵变成2行3列
t(mat) t(mat)
# => # =>
# [,1] [,2] [,3] # [,1] [,2] [,3]
# [1,] 1 2 3 # [1,] 1 2 3
# [2,] 4 5 6 # [2,] 4 5 6
# cbind() sticks vectors together column-wise to make a matrix # 使用 cbind() 函数把两个矩阵按列合并,形成新的矩阵
把两个矩阵按列合并,形成新的矩阵
mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog")) mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog"))
mat2 mat2
# => # =>
@ -385,16 +367,12 @@ mat2
# [3,] "3" "bird" # [3,] "3" "bird"
# [4,] "4" "dog" # [4,] "4" "dog"
class(mat2) # matrix class(mat2) # matrix
#定义mat2矩阵
# Again, note what happened! # Again, note what happened!
#同样的注释 # 注意
# Because matrices must contain entries all of the same class, # 因为矩阵内部元素必须包含同样的类型
#矩阵必须包含同样的形式 # 所以现在每一个元素都转化成字符串
# everything got converted to the character class
#每一个变量都可以转化成字符串形式
c(class(mat2[,1]), class(mat2[,2])) c(class(mat2[,1]), class(mat2[,2]))
# rbind() sticks vectors together row-wise to make a matrix
# 按行合并两个向量,建立新的矩阵 # 按行合并两个向量,建立新的矩阵
mat3 <- rbind(c(1,2,4,5), c(6,7,0,4)) mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
mat3 mat3
@ -402,19 +380,14 @@ mat3
# [,1] [,2] [,3] [,4] # [,1] [,2] [,3] [,4]
# [1,] 1 2 4 5 # [1,] 1 2 4 5
# [2,] 6 7 0 4 # [2,] 6 7 0 4
# Aah, everything of the same class. No coercions. Much better. # 哈哈,数据类型都一样的,没有发生强制转换,生活真美好
# TWO-DIMENSIONAL (DIFFERENT CLASSES) # 二维(不同的元素类型)
##二维函数(不同的变量类型)
# For columns of different classes, use the data frame # 利用 data frame 可以将不同类型数据放在一起
利用数组可以将不同类型放在一起
dat <- data.frame(c(5,2,1,4), c("dog", "cat", "bird", "dog")) dat <- data.frame(c(5,2,1,4), c("dog", "cat", "bird", "dog"))
#dat<-数据集(c(5,2,1,4), c("dog", "cat", "bird", "dog")) names(dat) <- c("number", "species") # 给数据列命名
names(dat) <- c("number", "species") # name the columns
#给每一个向量命名
class(dat) # "data.frame" class(dat) # "data.frame"
#建立数据集dat
dat dat
# => # =>
# number species # number species
@ -425,17 +398,15 @@ dat
class(dat$number) # "numeric" class(dat$number) # "numeric"
class(dat[,2]) # "factor" class(dat[,2]) # "factor"
# The data.frame() function converts character vectors to factor vectors # The data.frame() function converts character vectors to factor vectors
#数据集,将字符特征转化为因子矢 # data.frame() 会将字符向量转换为 factor 向
# There are many twisty ways to subset data frames, all subtly unalike # 有很多精妙的方法来获取 data frame 的子数据集
#这里有许多种生成数据集的方法,所有的都很巧妙但又不相似
dat$number # 5 2 1 4 dat$number # 5 2 1 4
dat[,1] # 5 2 1 4 dat[,1] # 5 2 1 4
dat[,"number"] # 5 2 1 4 dat[,"number"] # 5 2 1 4
# MULTI-DIMENSIONAL (ALL OF ONE CLASS) # 多维(相同元素类型)
#多维函数
# Arrays creates n-dimensional tables
# 利用数组创造一个 n 维的表格 # 利用数组创造一个 n 维的表格
# You can make a two-dimensional table (sort of like a matrix) # You can make a two-dimensional table (sort of like a matrix)
#你可以建立一个2维表格(类型和矩阵相似) #你可以建立一个2维表格(类型和矩阵相似)