函数名 | 功能 | 例子 | 例子效果 |
print() | 打印 | print ( "hello world") | "hello world" |
c() | 把多个向量拼成数组 | apple<-c("red","green"); print(apple); print(class(apple)) |
[1] "red" "green" |
list() | 组合出列表变量 | list1<-list(c(2,5,3),21.3,sin); print(list1) |
[[1]] [[2]] [[3]] |
matrix() | 生成 矩阵型 变量 | M=matrix(c('a','a','b'),nrow=2,ncol=3,byrow=TRUE); print(M) | [,1] [,2] [,3] [1,] "a" "a" "b" [2,] "a" "a" "b" |
if() {} | 决策 | x<-30L; if(is.integer(x)) {print("X is an Integer") } | "X is an Integer" |
if() {} else {} | 决策 | ||
if ... else if ... else | 决策 | ||
switch(exp , case1, case2, case3...) | 决策 | x<-switch(3,"one","two","three"); print(x) | "three" |
repeat { commands if(condition) { break } } | 循环 | v <- c("Hello","loop") cnt <- 2 repeat { print(v) cnt <- cnt+1 if(cnt > 5) { break } } |
[1] "Hello" "loop" |
while |
一次又一次地执行相同的代码,直到满足停止条件 while (exp) { statement } |
v <- c("Hello","while loop") cnt <- 2 while (cnt < 7) { print(v) cnt = cnt + 1 } |
[1] "Hello" "while loop" |
for | for (exp) { statement } | v <- LETTERS[1:4]; for ( i in v) { print(i) } |
[1] "A" |
cbind() | 连接多个向量,每个向量为一列 |
city <- c("Tampa","Seattle","Hartford","Denver")
|
city state zipcode |
rbind() | 上下合并两个数据帧 (类似 sql 的union all) |
city <- c("Tampa","Seattle","Hartford","Denver")
|
city state zipcode |
merge() | 合并两个数据帧,数据帧必须具有相同的列名,在其上进行合并 | ||