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() 合并两个数据帧,数据帧必须具有相同的列名,在其上进行合并  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

转载于:https://www.cnblogs.com/Elanlalala/p/10484170.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值