R语言

R语言--基础(二)

2018-11-21  本文已影响13人  _凌浩雨
1. 决策
Sr.No. 声明和描述
1 if语句: 由一个布尔表达式后跟一个或多个语句组成
2 if ... else语句: if语句后面可以有一个可选的else语句,当布尔表达式为false时执行
3 switch语句: 允许根据值列表测试变量的相等性。
# if语句
x = 30L
if(is.integer(x)) {
    print("X is an Integer")
}

打印结果:


图2.png
# if...else...
x = c("what", "is", "truth")
if("Truth" %in% x) {
    print("Truth is found")
} else {
    print("Truth is not found")
}

打印结果:


图4.png

示例:

# switch语句
x = switch(
        3,
        "first",
        "second",
        "third",
        "fourth"
    )
print(x)

打印结果:


图6.png
2. 包
# 获取R包路径
.libPaths()

# 获取所有软件包列表
library()
图7.png 图8.png
# 获取当前R环境中加载的所有包
search()

打印结果:


图9.png
# 从CRAN网页获取软件包
install.packages("spe", repos="https://cran.cnr.berkeley.edu/")

执行结果:

图10.png
II. 手动安装包
下载安装包
# 手动安装
install.packages("C:\\Users\\mazaiting\\Desktop\\spec_0.1.3.zip", repos = NULL, type = "source")

执行结果:


图11.png
library("package Name", lib.loc = "path to library")
3. 循环
Sr.No. 循环类型和描述
1 repeat循环:多次执行一系列语句,并简化管理循环变量的代码。
2 while循环:在给定条件为真时,重复语句或语句组。 它在执行循环体之前测试条件。
3 for循环:像while语句,不同之处在于它测试在循环体的端部的条件。

I. repeat循环


图12.png

示例:

# repeat语句
v = c("Hello","loop")
cnt = 2

repeat {
   print(v)
   cnt = cnt+1
   
   if(cnt > 5) {
      break
   }
}

打印结果:


图13.png

II. while循环


图14.png

示例:

# while循环语句
v = c("Hello","while loop")
cnt = 2

while (cnt < 7) {
   print(v)
   cnt = cnt + 1
}

打印结果:


图15.png

III. for循环


图16.png

示例:

# for循环语句
v <- LETTERS[1:4]
for ( i in v) {
   print(i)
}

打印结果:


图17.png
Sr.No. 控制语句和描述
1 break语句:终止循环语句,并将执行转移到循环后立即执行的语句。
2 next语句:next语句模拟R语言switch语句的行为。

I. break语句


图18.png

示例:

# break语句
v <- c("Hello","loop")
cnt <- 2

repeat {
   print(v)
   cnt <- cnt + 1
    
   if(cnt > 5) {
      break
   }
}

打印结果:


图19.png

II. next语句


图20.png

示例:

# next语句
v <- LETTERS[1:6]
for ( i in v) {
   
   if (i == "D") {
      next
   }
   print(i)
}

打印结果:


图21.png
4. 数据重塑
# 数据帧加入列和行
# 创建向量对象
city = c("Tampa", "Seattle", "Hartford", "Denver")
state = c("FL", "WA", "CT", "CO")
zipcode = c(33602, 98104, 06161, 80294)
# 将三个向量联合为一个数据帧
addresses = cbind(city, state, zipcode)
# 打印第一个输出提示
cat("# # # # The first data frame 
")
# 打印第一个数据帧
print(addresses)
# 创建另一个数据帧
new.address = data.frame(
    city = c("Lowry", "Charlotte"),
    state = c("CO", "FL"),
    zipcode = c("80230", "33949"),
    stringsAsFactors = FALSE
)
# 打印第二个输出提示
cat("# # # The Second data frame
")
# 打印数据帧
print(new.address)
# 从数据帧中合并行
all.addresses = rbind(addresses, new.address)
# 打印第三个提示
cat("# # # The combined data frame
")
# 打印结果
print(all.addresses)

打印结果:


图22.png
# 合并数据帧
library(MASS)
merged.Pima = merge(
    x = Pima.te, 
    y = Pima.tr,
    by.x = c("bp", "bmi"),
    by.y = c("bp", "bmi")
    # by.x = c("skin", "glu"),
    # by.y = c("skin", "glu")
)
# 打印合并后的数据
print(merged.Pima)
# 打印行数
nrow(merged.Pima)

打印结果:


图23.png
library(MASS)
# 打印船舶数据集
print(ships)

打印结果:


图24.png
# 此包中提供melt()和cast(), 需要下载
# install.packages("reshape2", repos = "https://cran.cnr.berkeley.edu/")
# melt()拆分数据
library(reshape2)
library(MASS)

molten.ships = melt(ships, id = c("type", "year"))
print(molten.ships)

打印结果:


图25.png
library(reshape2)
library(MASS)

molten.ships = melt(ships, id = c("type", "year"))
# print(molten.ships)

# dcast() 重构数据
recasted.ship = dcast(molten.ships, type+year~variable,sum)
print(recasted.ship)

打印结果:


图26.png
5. 函数
function_name <- function(arg_1, arg_2, ...) {
   Function body 
}
# 内置函数
# 创建一串数字,从32到44
print(seq(32, 44))

# 中间数
print(mean(25:82))

# 从41加到68
print(sum(41:68))

打印结果:


图27.png
# 用户定义的函数
new.function <- function(a) {
    for (i in 1:a) {
        b <- i^2
        print(b)
    }
}

# 调用函数
new.function(6)

打印结果:


图28.png

代码下载

上一篇 下一篇

猜你喜欢

热点阅读