CLT
-a lists hidden and unhidden files and folders
-al lists details for hidden and unhidden files and folders
cd takes as an argument the directory you want to visit
cd with no argument takes you to your home directory
cd .. allows you to chnage directory to one level above your current directory
cp takes as its first argument a file, and as its second argument the path to where you want the file to be copied
cp can also be used for copying the contents of directories, but you must use the -r flag
use rm to delete entire directories and their contents by using the -r flag
move files between directories
use mv to rename files
git
$ git config --global user.name "Your Name Here" # 输入用户名
$ git config --global user.email "your_email@example.com" # 输入邮箱
$ git config --list # 检查
$ git init # 初始化目录
$ git add . # 添加新文件
$ git add -u # 更新改名或删除的文件
$ git add -A|git add --all # 添加所有改动
$ git commit -m "your message goes here" # 描述并缓存本地工作区改动到上一次commit
$ git log # 查看commit记录 用Q退出
$ git status # 查看状态
$ git remote add # 添加服务器端地址
$ git remote -v # 查看远端状态
$ git push # 将本地commit推送到github服务器端
$ git pull|fetch|merge|clone # 本地获取远端repo
$ exit # 退出
基本问题
数据次于问题
大数据依赖科学而不是数据
实验设计 重视可重复性随机与分组 预测与推断不同 不要选数据
help()
`?`(command)
# 提问给出以下信息
version
str(.Platform)
Re实部 Im虚部) 逻辑[[*]]引用相应向量 unlist 可用做紧凑输出attributesa <- b <- c#表示注释 不执行: 用来产生整数序列 也可以用seq生成c产生vector()函数建立as.*来强制转化数据类型names命名FALSE Inf NA NaN NULL TRUE break else for function if in next repeat while
dimension属性的向量为矩阵 矩阵的生成次序为upper-leftmatrix(1:6,nrow=2,ncol=3)表示建一个2行3列矩阵 从1到6 先列后行赋值 可用 byrow = T 来更改c给dim赋值行和列数 这样可把一个向量转为一个矩阵 m<-1:6;dim(m)<-c(2,3)rbind或cbind生成t对矩阵转置level来命名排序 默认是字母排序 有些函数对顺序敏感可用 levels = c() 来命名 ( 例如低中高的排序 ) 数字表示 drop = T 表示显示截取数据的水平 nlevels给出个数row.namesdata.matrixI data.frame(x,y,I(c))dim() = c(x,y,z) 三维数组表示一组数 dimnames 给数组命名 drop = F 否则 不会按照数组分类ts 产生时间序列对象.Last.value 引用前一个数值round(x,n) n表示保留几位小数truncsqrtabsexploglog10sin cos tan asin acos atan> 小于 < 等于 == 小于或等于 <= 大于或等于 >= 与 & 非 ! 或|x %in% y 结果返回逻辑值sum 求和 prod 求连乘range 给极值范围duplicated 给出有重复的值 unique 给出无重复的值union 并集 intersect 交集 setdiff 除了交集的部分rep 用向量循环生成向量x <- 1:4 # puts c(1,2,3,4) into x
i <- rep(2, 4) # puts c(2,2,2,2) into i
y <- rep(x, 2) # puts c(1,2,3,4,1,2,3,4) into y
z <- rep(x, i) # puts c(1,1,2,2,3,3,4,4) into z
w <- rep(x, x) # puts c(1,2,2,3,3,3,4,4,4,4) into w
[]截取数据[x,y]提取特定数值[-1,-2]可剔除第一行第二列[[]]用来从list或者frame里提取元素 类型固定 可提取序列x[[1]][[3]] 可部分匹配 exact=FALSEx[1,2,drop=FALSE]is.NA()提取 用!排除 缺失值可用is.element(x,y)来处理很多表示NA值的数字 返回x %in% y的逻辑值complete.cases()提取有效数据用[]提取可用数据head(x,n) n表示从头截取多少行tail(x,n) n表示从尾截取多少行subset(x,f) x表示数据 f表示表达式[is.element(x,c(' ',' ',' ')),] 或者[x%in%c(' ',' ',' '),] 使用x == c( ' ' , ' ' , ' ' ) 会报错 循环查找三个变量 x!='t' 可能会把空白值输入 应该使用is.element(x,'t')ifelse(con,yes,no) 利用条件筛选 返回yes 或者no 的值read.table read.csv 读取表格 反之write.tablereadLines 读取文本行 反之writeLinessource 读取R代码 反之dumpdget 读取多个R代码 反之dputload 读取保存的工作区 反之saveunserialize 读取二进制R对象 反之serialize
设置工作目录
getwd()
setwd()
?read.table comment.char = "" 不扫描注释nrowscolClassesinitial <- read.table("datatable.txt", nrows = 100)
classes <- sapply(initial, class)
tabAll <- read.table("datatable.txt", colClasses = classes)
connections与file等保存外部文件指向if else 条件if(<condition>) {
## do something
} else {
## do something else
}
if(<condition1>) {
## do something
} else if(<condition2>) {
## do something different
} else {
## do something different
}
for (i in 1:10) {
print(i)
}
while 条件为真执行循环 条件从左到右执行count <- 0
while (count < 10) {
print(count)
count <- count + 1
}
repeat 执行无限循环 配合break 中断并跳出循环next 跳出当前循环继续执行for (i in 1:100) {
if (i <= 20) {
## Skip the first 20 iterations
next
}
## Do something here
}
return 退出函数apply替代f <- function(<arguments>) {
## Do something interesting
}
formals()显示NULL... 向其他函数传参 之后参数不可部分匹配%*% 表示矩阵操作data类型存储POSIXct 或 POSIXlt 类型存储POSIXct以整数存储时间POSIXlt以年月日时分秒等信息存储时间strptime as.Date as.POSIXlt as.POSIXct用来更改字符为时间lapplyx <- list(a = 1:5, b = rnorm(10))
lapply(x, mean)
## $a
## [1] 3
##
## $b
## [1] 0.09485
x <- 1:4
lapply(x, runif, min = 0, max = 10)
## [[1]]
## [1] 8.89
##
## [[2]]
## [1] 5.639 1.004
##
## [[3]]
## [1] 3.834 9.982 1.325
##
## [[4]]
## [1] 2.674 3.279 1.805 5.022
x <- list(a = matrix(1:4, 2, 2), b = matrix(1:6, 3, 2))
lapply(x, function(elt) elt[, 1])
## $a
## [1] 1 2
##
## $b
## [1] 1 2 3
sapplylapply的精简版 x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
sapply(x, mean)
## a b c d
## 2.5000 0.3861 1.0057 4.9518
vapplylapply可用更复杂函数 返回矩阵replicaterapplyhow来调整输出方法 如选取某列表中类型数据进行迭代applyrowSums rowMeans colSums colMeans 来替代 大数据量更快x <- matrix(rnorm(50), 10, 5)
apply(x, 1, quantile, probs = c(0.25, 0.75))
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## 25% -0.4016 -0.3194 -1.4145 -1.3172 -0.9657 -1.339 -0.9349 -0.07081
## 75% 0.5756 0.8038 -0.1331 -0.8333 0.3405 -0.174 -0.5182 1.01415
## [,9] [,10]
## 25% -0.1032 -0.1605
## 75% 0.1423 0.4605
a <- array(rnorm(2 * 2 * 10), c(2, 2, 10))
apply(a, c(1, 2), mean)
## [,1] [,2]
## [1,] -0.1631 0.02116
## [2,] 0.2227 -0.02247
tapplyx <- c(rnorm(10), runif(10), rnorm(10, 1))
f <- gl(3, 10)
tapply(x, f, mean)
## 1 2 3
## -0.1923 0.5339 0.2723
bytapply by(x[,-a],a,mean)splitlapply使用tapplydrop来删除空分组x <- c(rnorm(10), runif(10), rnorm(10, 1))
f <- gl(3, 10)
lapply(split(x, f), mean)
## $`1`
## [1] -0.3494
##
## $`2`
## [1] 0.3945
##
## $`3`
## [1] 1.113
x <- rnorm(10)
f1 <- gl(2, 5)
f2 <- gl(5, 2)
str(split(x, list(f1, f2), drop = TRUE))
## List of 6
## $ 1.1: num [1:2] -0.356 -0.175
## $ 1.2: num [1:2] -0.527 0.394
## $ 1.3: num 0.233
## $ 2.3: num -0.166
## $ 2.4: num [1:2] -0.96 -0.735
## $ 2.5: num [1:2] -0.321 0.208
mapplyapply 从多个参数范围取值 并用函数得到结果noise <- function(n, mean, sd) {
rnorm(n, mean, sd)
}
mapply(noise, 1:5, 1:5, 2)
## [[1]]
## [1] 0.5623
##
## [[2]]
## [1] 3.762 1.500
##
## [[3]]
## [1] 2.163 5.055 2.814
##
## [[4]]
## [1] 0.1819 2.3478 6.1354 5.4761
##
## [[5]]
## [1] 5.61986 5.20469 3.63678 -0.06725 6.53935
# 等同于如下循环
# list(noise(1, 1, 2), noise(2, 2, 2), noise(3, 3, 2), noise(4, 4, 2),
# noise(5, 5, 2))
eapplydnorm(x, mean = 0, sd = 1, log = FALSE)
pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean = 0, sd = 1)
set.seed保证重现性sample对数据采样message warning error 只有error致命traceback debug browser trace recoversystem.time 计算代码运行时间 返回对象类型proc_time
system time CPU时间elapsed time 实际用时Rprof R代码要支持分析函数
summaryRprof可使结果易读system.time混用by.total 记录单个函数用时by.self 记录函数执行时被调用函数用时