R-函数/语法-整合版

函数名 功能 例子 例子效果
print()  打印  print ( "hello world") "hello world"
c()  把多个向量拼成数组  apple<-c("red","green"); print(apple); print(class(apple))  

[1] "red" "green"
[1] "character"

list()   组合出列表变量  list1<-list(c(2,5,3),21.3,sin); print(list1)  

[[1]]
[1] 2 5 3

[[2]]
[1] 21.3

[[3]]
function (x) .Primitive("sin")

 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"
[1] "Hello" "loop"
[1] "Hello" "loop"
[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"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"

 for  for (exp) { statement }  v <- LETTERS[1:4]; for ( i in v) { print(i) }  

[1] "A"
[1] "B"
[1] "C"
[1] "D"

 cbind() 连接多个向量,每个向量为一列  

city <- c("Tampa","Seattle","Hartford","Denver")
state <- c("FL","WA","CT","CO")
zipcode <- c(33602,98104,06161,80294)


addresses <- cbind(city,state,zipcode); print(address)

 

         city    state   zipcode
[1,] "Tampa" "FL" "33602"
[2,] "Seattle" "WA" "98104"
[3,] "Hartford" "CT" "6161"
[4,] "Denver" "CO" "80294"

 rbind() 上下合并两个数据帧 (类似 sql 的union all)  

city <- c("Tampa","Seattle","Hartford","Denver")
state <- c("FL","WA","CT","CO")
zipcode <- c(33602,98104,06161,80294)


all.addresses <- rbind(addresses,addresses); print(all.addresses)

 

          city    state  zipcode
[1,] "Tampa" "FL" "33602"
[2,] "Seattle" "WA" "98104"
[3,] "Hartford" "CT" "6161"
[4,] "Denver" "CO" "80294"
[5,] "Tampa" "FL" "33602"
[6,] "Seattle" "WA" "98104"
[7,] "Hartford" "CT" "6161"
[8,] "Denver" "CO" "80294"

 merge()  合并两个数据帧,数据帧必须具有相同的列名,在其上进行合并    
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
posted @ 2019-03-06 16:38  Elan-_-lalala  阅读(240)  评论(0编辑  收藏  举报