1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-17 20:11:57 +02:00

transalted 50%

This commit is contained in:
alswl
2013-09-18 21:26:38 +08:00
parent 8782aded63
commit 6b9ac70e5e

View File

@@ -204,115 +204,76 @@ c(6, 8, 7, 5, 3, 0, 9)[3] # 7
# CHARACTERS # 字符串
#特性
# There's no difference between strings and characters in R
# 字符串和字符在 R 语言中没有区别 # 字符串和字符在 R 语言中没有区别
"Horatio" # "Horatio" "Horatio" # "Horatio"
#字符输出"Horatio"
class("Horatio") # "character" class("Horatio") # "character"
#字符串输出("Horatio") # "character"
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis " substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
#提取字符串("Fortuna multis dat nimis, nulli satis.", 第9个到15个之前并输出)
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis." gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
#替换字符春用ø替换u
# LOGICALS
# 逻辑值 # 逻辑值
# booleans # 布尔值
#布尔运算
class(TRUE) # "logical" class(TRUE) # "logical"
#定义为真,逻辑型
class(FALSE) # "logical" class(FALSE) # "logical"
#定义为假,逻辑型 # 和我们预想的一样
# Behavior is normal
#表现的标准形式
TRUE == TRUE # TRUE TRUE == TRUE # TRUE
TRUE == FALSE # FALSE TRUE == FALSE # FALSE
FALSE != FALSE # FALSE FALSE != FALSE # FALSE
FALSE != TRUE # TRUE FALSE != TRUE # TRUE
# Missing data (NA) is logical, too # 缺失数据NA也是逻辑值
#缺失数据也是逻辑型的
class(NA) # "logical" class(NA) # "logical"
#定义NA为逻辑型 #定义NA为逻辑型
# FACTORS
# 因子 # 因子
# The factor class is for categorical data # 因子是为数据分类排序设计的(像是排序小朋友们的年级或性别)
#因子是分类数据的定义函数
# which can be ordered (like childrens' grade levels)
#可以使有序的(就像儿童的等级水平)
# or unordered (like gender)
#也可以是无序的(就像性别)
levels(factor(c("female", "male", "male", "female", "NA", "female"))) # "female" "male" "NA" levels(factor(c("female", "male", "male", "female", "NA", "female"))) # "female" "male" "NA"
#c("female", "male", "male", "female", "NA", "female")向量变量是字符型levels factor因子的等级水平
factor(c("female", "female", "male", "NA", "female")) factor(c("female", "female", "male", "NA", "female"))
# female female male NA female # female female male NA female
# Levels: female male NA # Levels: female male NA
data(infert) #Infertility after Spontaneous and Induced Abortion data(infert) # 自然以及引产导致的不育症
#数据集(感染) 自然以及引产导致的不育症
levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs" levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs"
#等级(感染与教育程度) 输出
# VARIABLES
# 变量 # 变量
# Lots of way to assign stuff # 有许多种方式用来赋值
#许多种方式用来分配素材 x = 5 # 这样可以
x = 5 # this is possible y <- "1" # 更推荐这样
#x = 5可能的 TRUE -> z # 这样可行,但是很怪
y <- "1" # this is preferred
#y <- "1" 优先级的
TRUE -> z # this works but is weird
#输出真实的,存在一个超自然数满足条件
# We can use coerce variables to different classes #我们还可以使用强制转型
#我们还可以使用枪支变量去进行不同的定义
as.numeric(y) # 1 as.numeric(y) # 1
#定义数值型
as.character(x) # "5" as.character(x) # "5"
#字符型
# LOOPS
# 循环 # 循环
# We've got for loops # for 循环语句
#循环语句
for (i in 1:4) { for (i in 1:4) {
print(i) print(i)
} }
#定义一个i从1-4输出
# We've got while loops # while 循环
#我们可以获取循环结构
a <- 10 a <- 10
while (a > 4) { while (a > 4) {
cat(a, "...", sep = "") cat(a, "...", sep = "")
a <- a - 1 a <- a - 1
} }
#把10负值为aa4输出文件a,"...",sep="" ),跳出继续下一个循环取a=a-1,如此循环直到a=10终止
# Keep in mind that for and while loops run slowly in R # 记住,在 R 语言中 for / while 循环都很慢
#在R语言中牢记 for和它的循环结构 # 建议使用 apply()(我们一会介绍)来错做一串数据(比如一列或者一行数据)
# Operations on entire vectors (i.e. a whole row, a whole column)
#牢记矢量中附带的操作(例如,整行和整列)
# or apply()-type functions (we'll discuss later) are preferred
#或者优先使用()-函数,稍后会进行讨论
# IF/ELSE # IF/ELSE
#判断分支
# Again, pretty standard # 再来看这些优雅的标准
#再一次,看这些优雅的标准
if (4 > 3) { if (4 > 3) {
print("Huzzah! It worked!") print("Huzzah! It worked!")
} else { } else {
@@ -322,26 +283,21 @@ if (4 > 3) {
# => # =>
# [1] "Huzzah! It worked!" # [1] "Huzzah! It worked!"
# FUNCTIONS # 函数
#功能函数
# Defined like so:
# 定义如下 # 定义如下
jiggle <- function(x) { jiggle <- function(x) {
x + rnorm(x, sd=.1) #add in a bit of (controlled) noise x + rnorm(x, sd=.1) #add in a bit of (controlled) noise
return(x) return(x)
} }
#把功能函数x负值给jiggle
# Called like any other R function: # 和其他 R 语言函数一样调用
jiggle(5) # 5±ε. After set.seed(2716057), jiggle(5)==5.005043 jiggle(5) # 5±ε. 使用 set.seed(2716057) jiggle(5)==5.005043
######################### #########################
# Fun with data: vectors, matrices, data frames, and arrays # 数据容器:vectors, matrices, data frames, and arrays
# 数据参数:向量,矩阵,数据框,数组,
######################### #########################
# ONE-DIMENSIONAL
# 单维度 # 单维度
# You can vectorize anything, so long as all components have the same type # You can vectorize anything, so long as all components have the same type
#你可以将任何东西矢量化,因此所有的组分都有相同的类型 #你可以将任何东西矢量化,因此所有的组分都有相同的类型