R语言

R语言学习

2018-12-25  本文已影响19人  214b3ff96d82

R语言基础入门

良好的R使用习惯

设置R默认启动项
file.edit('~/.Rprofile')
#bioconductor
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
#CRAN
options(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")

R包管理
install.packages("ggplot2")  安装R包
library(ggplot2)  加载R包
update.packages()  将所有的包进行更新

更新R版本
install.packages("installr")
require(installr)
updateR()
R中的数据对象与数据操作
赋值与注释
a = 2  # R中对变量的名字大小写敏感,a与A是不同的
a <- 2
a <- 2
b <- 2
c <- a + b
数据对象

数据对象通常有三个属性:类型、值、变量名称


举例
  1. 标量
a <- 1
a = 3.14
m <- a + b
x <- F
TURE -> y
> y
[1]  TURE
> x
[1]  FALSE
  1. 向量
a <- c(1,2,3,4,5)
b <- c("one","two","three")
c <- c(TURE,FALSE,FALSE,TURE)
# 使用函数rep(), seq(), ":"生成向量
d <- rep(2,times=4)
> d
[1] 2 2 2 2
e <- seq(from=3,to=21,by=3)
> e
[1]  3  6  9 12 15 18 21
f <- c(1:10)
> f
 [1]  1  2  3  4  5  6  7  8  9 10
# 向量提取元素
> a[1]
[1] 1
> a[c(1,5)]
[1] 1 5
> a[1:3]
[1] 1 2 3
> a[a<5]
[1] 1 2 3 4  # 通过判断来提取元素
> a[a>5]
[1]  6  7  8  9 10
> a[-c(1,5)]
[1]  2  3  4  6  7  8  9 10   # 去除向量中的元素
a[5] <- 12
> a
 [1]  1  2  3  4 12  6  7  8  9 10  #替换元素
a <- append(a,12,5)
> a
 [1]  1  2  3  4  5 12  6  7  8  9 10   # 添加元素
  1. 因子
# 因子是一种特殊类型的字符型向量
# 创建一个字符向量或整数向量
# 使用 factor()函数将其转为因子
b <- c("one","two","three")
b <- factor(b)
> b
[1] one   two   three
Levels: one three two
  1. 矩阵
矩阵是一个二维数组
函数matrix()用于创建矩阵
y <- matrix(1:20,nrow=5,ncol=4)
> y
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
y <- matrix(1:20,nrow=5,ncol=4,byrow = T)
> y
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16
[5,]   17   18   19   20
  1. 数据框
patientID <- c(1:4)
age <- c(25,34,28,52)
status <- c("poor","improved","poor","good")
patientDate <- data.frame(patientID,age,status)
> patientDate
  patientID age   status
1         1  25     poor
2         2  34 improved
3         3  28     poor
4         4  52     good
> NCBIdata
   Year BasePairs
1  1991      1100
2  1992      1200
3  1993      1300
4  1994      1400
5  1995      1500
6  1996      1600
7  1997      1700
8  1998      1800
9  1999      1900
10 2000      2000

# 按照条件提取
> NCBIdata[NCBIdata$Year>1998,]
   Year BasePairs
9  1999      1900
10 2000      2000
 NCBIdata[NCBIdata$Year>1990 & NCBIdata$BasePairs>1700,]
   Year BasePairs
8  1998      1800
9  1999      1900
10 2000      2000
> NCBIdata[,c("Year")]
 [1] 1991 1992 1993 1994 1995 1996
 [7] 1997 1998 1999 2000

  1. 列表
rec <- list(name="LiMing",age=18,scores=c(85,76,90))

> rec
$`name`
[1] "LiMing"

$age
[1] 18

$scores
[1] 85 76 90
R中的基础函数及编程

函数形式: 函数(输入数据,参数=)
平均值: mean(x,trim=0,na.rm =FALSE,...)
线性模型: lm(y~x,data=test)

R字符数据操作函数
>paste("abc","bc")
[1] "abc bc"
>paste("abc","bc",sep="")
[1] "abcbc"
>nchar("abcccc")
[1] 6
> substr("abcdef",2,3)
[1] "bc"
> substring("abcdef", 3)
[1] "cdef"
substring("abcdef", 1:6, 1:6)
[1] "a" "b" "c" "d" "e" "f"
> substr(rep("abcdef", 4), 1:4, 4:5)
[1] "abcd" "bcde" "cd"   "de"  
编程基础

函数名 <- function (数据,参数1=默认值,...)
{
异常处理;
表达式(循环/判别);
return(返回值);
}

输入直角三角形的两个边,求斜边长
rcal <- function(x,y){
      z <- x^2 + y^2
      result <- sqrt(z)
      return(result)
}

rcal(3,4)
流程控制 if

if (条件)表达式,或if (条件)表达式1 else 表达式2

p = 0.03
if (p < = 0.05) {
      print("p <= 0.05!")
} else {
      print("p > 0.05!")
}

x = 3
y <- if (x==2) x else x+1
条件与逻辑运算
循环 for,while
for (i in 1:10)  print (i)
a <- 0
for (i in 1:10){
      a <- a+1
}
> a
[1] 10

i <- 1
while (i <10) { print (i); i <- i + 1}

数据的输入与输出
read.table()
read.csv()
write.table()
write.csv()
上一篇 下一篇

猜你喜欢

热点阅读