R语言使用简介

R 的基本介绍

R 语言基于 S 语言,是一种免费的用于数据分析的语言。

R系统从概念上分为两部分:

  1. 从 CRAN上下载的基础的R 系统;
  2. 其它

R 的功能基本上是由各种不同的 package 提供实现的:

  • base包提供 R 的基本功能;
  • R 的基础部分包括这些 packages:utils, stats, datasets, graphics, grDevices, grid, methods, tools, parallel, compiler, splines, tcltk, stats4
  • 其它一些推荐安装的 packages:boot, class, cluster, codetools, foreign, KernSmooth, lattice, mgcv, nlme, rpart, survival, MASS, spatial, nnet, Matrix

R 的 Input 和 Evaluation

R 使用**<-符号作为赋值操作,=**也可以。


x<-1
可以使用xprint(x)显示 x 的值。

  • x:自动显示
  • print(x):明确指定显示

当输入一个表达式变量并回车后,变量即被 evaludated ,并返回变量的结果。

x<-1:20

**:**符号表示建立一个整形的序列

R 的数据类型

对象类型

R的5种基本(atomic)的数据类型:

  1. character
  2. numeric(real numbers)
  3. integer
  4. complex
  5. logical(True/False)

常用的对象类型有 vector,vector 只能包含相同的数据类型,但有一个例外,就是 list,它以 vector 形式显示,但可以保存不同的数据类型。

空 vector 可以用**vector()**表示。

Numbers

R 中的数字默认为numberic 类型,即双精度浮点数,如果是整型数据,需要指定 L 后缀。例如1是一个numberic 对象,而1L 则是integer。

Inf 代表无限,1/0的结果是 Inf。Inf 可以用来计算中,如1/Inf=0。

NaN代表未定义的,如0/0,也可以代表缺失的值。

Vectors

建立 vector对象,可以使用 **c()**函数进行,如:

x<-c(0.5,0.6)        ##numeric
x<-c("a","b","c")    ##character
x<-c(TRUE,FALSE)    ##logical
x<-c(12:23)        ##integer
x<-c(1+0i,2-4i)    ##complex

也可以用vector()初始化对象,如:

x<-vector("numeric",length=10)
x
[1] 0 0 0 0 0 0 0 0 0 0

当不同的对象类型混合在一个 vector 里的时候,会发生coercion(胁迫),将 vector 内的对象转换成相同类型的对象。

y=c(1.7,"a")
y
[1] "1.7" "a"

可以看到,numeric 类型的1.7被强制转换成了”1.7”。这种是自动的转换数据类型。

还可以指定明确的转换类型,使用**as.***来进行转换:

x=0:6
class(x)
结果:
[1] "integer"

as.logical(x)
结果:
[1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

对于无意义的指定转换,结果为NA:

x <- c("a", "b", "c")
as.numeric(x)
[1]NA NA NA

Matrics

矩阵是有维度属性的vector,矩阵 matrics 的 dimension 属性是由一个长度为2的整数vector(nrow,ncol)组成。

创建 matrics 的几种方法:

m = matrix(nrow=2,ncol=3)

    [,1] [,2] [,3] 
[1,] NA NA NA 
[2,] NA NA NA

m = matrix(1:6,nrow = 2, ncol = 3)

    [,1] [,2] [,3] 
[1,] 1     3     5 
[2,] 2     4     6

m = 1:10
dim(m) = c(2,5)
    [,1] [,2] [,3] [,4] [,5] 
[1,] 1     3     5     7     9 
[2,] 2     4     6     8     10

还可以使用 cbind()和rbind()把 vector 拼成一个 matrics:

x = 1:3
y = 10:12
cbind(x,y)
    x    y 
[1,] 1 10 
[2,] 2 11 
[3,] 3 12

rbind(x,y)
    [,1] [,2] [,3] 
x    1    2    3 
y   10     11     12

Factors

Factors用来展现分类的数据,它可以是有顺序的也可以无序。可以把他们看作是一个整数的向量,其中每个整数有一个标签。
(类似枚举类型)

x <- factor(c("yes", "yes", "no", "yes", "no"))
[1] yes yes no yes no

table(x)
unclass(x)

Factors中的元素顺序可以通过 level 来设定,

x <- factor(c("yes", "yes", "no", "yes", "no"), 
                   levels = c("yes", "no"))

Missing Values

NA 和 NAN 用来代表未定义的数学操作

is.na()判断是否为 NA
is.nan()判断是否为 NaN

NA 值也有一个类型,所以存在integer的 NA,character 的 NA 等
NaN 也是 NA,但 NA 不是 NaN

x<-c(1,2,NA,10,3)
is.na(x)
[1] FALSE FALSE TRUE FALSE FALSE

is.nan(x)
[1] FALSE FALSE FALSE FALSE FALSE

x<-c(1,2,NaN,NA,4)
is.na(x)
[1] FALSE FALSE TRUE TRUE FALSE

is.nan(x)
[1] FALSE FALSE TRUE FALSE FALSE

Data Frames

Data Frames用来存储表格数据(tabular data)

通常,data frames 是由read.table()或read.csv()创建的。
可以使用data.matrix()转换为 matrix

x <- data.frame(foo = 1:4, bar = c(T, T, F, F)) >x
  foo bar
1 1 TRUE
2 2 TRUE
3 3 FALSE
4 4 FALSE

nrow(x) 
[1] 4

ncol(x) 
[1] 2

Names

可以对R 的对象进行命名操作

x<-1:3   ## 针对 vector
names(x)
NULL

names(x) <- c("foo", "bar", "norf") 
x
foo bar norf
1    2    3

names(x)
[1] "foo" "bar" "norf"

x<-list(a=1,b=2,c=3)  ## 针对 list

m <- matrix(1:4, nrow = 2, ncol = 2)  ##针对 matrices
dimnames(m) <- list(c("a", "b"), c("c", "d"))

R读写数据

  • read.table, read.csv用来读取表格数据,对应write.table
  • readLines读取文本文件的行,对应writeLines
  • source 读取 R 的 code 文件,对应dump
  • dget, 读取 R 文件,对应dput
  • load, 读取workspaces文件,对应save
  • unserialize, 读取R对象,对应serialize

read.table

read.table是最常用的读取数据的方法,有以下的参数:

  • file:文件名
  • header:logical值,是否有 header 行
  • sep:列分隔字符
  • colClasses: 一个character向量,说明数据集中每一列的类型
  • nrows:行数
  • comment.char:注释字符
  • skip:从文件头跳过的行数
  • stringsAsFactors:是否把字符变量转化为 factors

读取方法:

data <- read.table("foo.txt")

在读取过程中,R 会:

  • 会跳过所有以#开头的行
  • 会显示多少行,分配了多少内存
  • 说明每一列的变量类型
  • read.csv()也是一种 read.table(),只不过默认以,为分隔符

对于大数据,有一些方法可以提高读取的速度:

  1. 设置comment.char = “”,如果没有注释行
  2. 使用 colClasses参数,指定每一列的类型,这样读取速度可以提高一倍。如所有的列都是 numeric,可以设置 colClasses=”numeric”。

另一个 dirty 方法,可以解决不同列类型的表:

initial <- read.table("datatable.txt", nrows = 100) 
classes <- sapply(initial, class)
tabAll <- read.table("datatable.txt",colClasses = classes)

读取其它文件

file:读取文件
gzfile:读取 gz 文件
bzfile:读取 bz 文件
url:读取 url

con <- file("foo.txt", "r") 
data <- read.csv(con) 
close(con)

与
data <- read.csv("foo.txt")
相同

Subsetting 子集

[]:返回与原始数据相同的类,可一次读取多个值
[[]]:从 list 或 data frame 中读取数据
$:用 name 方法从 list或 data frame 中读取数据

三中读取值的方式的不同:

x <- list(foo = 1:4, bar = 0.6)
x[1]
$foo
[1]1234

x[[1]]
[1]1234

x$bar
[1] 0.6
x[["bar"]]
[1] 0.6
x["bar"]
$bar
[1] 0.6

还可以用 vector 取值:

x <- list(foo = 1:4, bar = 0.6, baz = "hello")
x[c(1, 3)]
$foo
[1]1234

$baz
[1] "hello"

[[索引值可以为变量,而$只能使用常量名称:

x <- list(foo = 1:4, bar = 0.6, baz = "hello")
name <- "foo"
x[[name]]
[1]1234

x$name
NULL

x$foo
[1] 1 2 3 4

The [[ can take an integer sequence.

x <- list(a = list(10, 12, 14), b = c(3.14, 2.81))
x[[c(1, 3)]]
[1] 14
x[[1]][[3]]
[1] 14

x[[c(2, 1)]]
[1] 3.14

Matrix使用[i,j]形式取值:

x <- matrix(1:6, 2, 3)
x[1, 2]
[1] 3

行或列可以缺失,就是取整列或整行:

x[1,]
[1]135

x[,2]
[1]34

当对 matrix 取单一元素值的时候,加上drop = FALSE,可以使返回值从 vector 变成 matrix:

 x <- matrix(1:6, 2, 3)
 x[1, 2]
 [1] 3
 
 x[1, 2, drop = FALSE]
     [,1] 
 [1,] 3

取整列的时候也是一样,drop=FALSE 可以把结果变成 matrix:

x <- matrix(1:6, 2, 3)
x[1, , drop = FALSE]

    [,1] [,2] [,3] 
[1,] 1     3     5

Partial Matching 部分匹配:

[[和$取值时允许参数部分匹配,如:

 x <- list(aardvark = 1:5)

 x$a
 [1]12345

 x[["a"]]
 NULL     

 x[["a", exact = FALSE]]
 [1]12345

删除 NA 值的方法:

x<-c(1,2,NA,4,NA,5)
bad <- is.na(x)
x[!bad]
[1]1245

complete.cases(x, y)

vector 的操作:

x<-1:4;y<-6:9
x+y
[1]791113

x>2
[1] FALSE FALSE TRUE TRUE

x <- matrix(1:4, 2, 2); y <- matrix(rep(10, 4), 2, 2)
x * y
    [,1] [,2] 
[1,] 10 30 
[2,] 20 40

R 语言的控制结构

条件

if-else 结构

if(<condition>) {
    ## do something
}else{
    ## do something else
}

if(<condition1>) {
    ## do something
} else if(<condition2>) {
    ## do something different
}else{
    ## do something different
}

循环

for 循环

x <- c("a", "b", "c", "d")
for(i in 1:4) {
    print(x[i])
}

for(i in seq_along(x)) 
{ 
    print(x[i])
}

for(letter in x) {
    print(letter)
}

while 循环:

count <- 0 
while(count < 10) {
    print(count)
    count <- count + 1
}    

repeat 循环:

x0<-1 
tol <- 1e-8

repeat {
    x1 <- computeEstimate()
    if(abs(x1 - x0) < tol) { 
        break    
    }else{ 
        x0<-x1
    } 
}

next 用于跳过本次循环,开始下一个循环;return 是直接返回

函数 function

格式为:

f <- function(<arguments>) {
    ## Do something interesting
}

函数的参数可以设定默认值

参数具有 lazy evaluation 的特性,如果某个参数在函数中没有用到,那调用时可以不传入该参数,函数不会报错。

…参数一般用来传递给内部调用的其它函数

Scoping Rules

符号与值的绑定

R把一个符号绑定到一个值上,是通过一系列的 environments找到合适的值。当在 shell 下使用一个变量时,大概的过程是:

  1. 查找全局的 environment, 是否有值匹配该变量符号;
  2. 在 search list 中,查找每个 package 的 namespace

search()可以查找当前的 search list 情况:

  • global environment或user workspace通常是 search list 的第一个元素,base包总是最后一个元素。
  • 当用户使用 library 加载时,该 package 的 namespace 会在 search list 的第2位上,其它的元素会相应后移。
  • R 对于函数和非函数有不同的 namespace,所以会同时有一个叫 c 的对象和叫 c 的函数。

Scoping Rules

scoping rules决定了在函数中,一个值如何与 free variable建立关联。

R 使用lexical scoping和static scoping,一个可选的方案dynamic scoping。

Scoping Rules 决定了 R如何使用search list为绑定符号和值。

Lexical Scoping

如下面的函数:

f <- function(x, y) { 
    x^2+y/z
}

变量 z 在函数参数中未定义,但是直接使用了,这种变量称为free variable。
Scoping Rules定义了这种 free variable 是如何赋值的

Lexical scoping的意思是:
the values of free variables are searched for in the environment in which the function was defined.

environment 是一组键值对的组合,a collection of (symbol, value) pairs
每个 environment 有它的父 environment,一个environment 可能有多个子 environment;
没有父 environment 的 environment,是一个空的 environment
function+environment = a closure or function closure

查找 free variable 的值的过程:

  • If the value of a symbol is not found in the environment in which a function was defined, then the
    search is continued in the parent environment.
  • The search continues down the sequence of parent environments until we hit the top-level environment; this usually the global environment (workspace) or the namespace of a package.
  • After the top-level environment, the search continues down the search list until we hit the empty environment. If a value for a given symbol cannot be found once the empty environment is arrived at, then an error is thrown.

R Scoping Rules

When a function is defined in the global environment and is subsequently called from the global environment, then the defining environment and the calling environment are the same. This can sometimes give the appearance of dynamic scoping.

  • In R, all objects must be stored in memory
  • All functions must carry a pointer to their respective defining environments, which could be anywhere
  • In S-PLUS, free variables are always looked up in the global workspace, so everything can be stored on the disk because the “defining environment” of all functions is the same.

Lexical scoping summary

  • Objective functions can be “built” which contain all of the necessary data for evaluating the function
  • No need to carry around long argument lists — useful for interactive and exploratory work.
  • Code can be simplified and cleand up

Coding Standards for R

  1. Always use text files / text editor
  2. Indent your code
  3. Limit the width of your code (80 columns?)
  4. Limit the length of individual functions

Date and Time

日期用 Date 类表示,它的内部是记录自从1970-1-1开始的天数

时间用POSIXct或者POSIXlt类表示,内部记录的是以从1970-1-1开始的秒数

x <- as.Date("1970-01-01")
  • POSIXct is just a very large integer under the hood; it use a useful class when you want to store times in something like a data frame
  • POSIXlt is a list underneath and it stores a bunch of other useful information like the day of the week, day of the year, month, day of the month

Times can be coerced from a character string using the as.POSIXlt or as.POSIXct function.

x <- Sys.time()
[1] "2013-01-24 22:04:14 EST"

p <- as.POSIXlt(x)
names(unclass(p))
[1] "sec" "min" "hour" "mday" "mon"
[6] "year" "wday" "yday" "isdst"
p$sec
[1] 14.34

strptime函数用来转换日期格式:

datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10") 
x <- strptime(datestring, "%B %d, %Y %H:%M")
x
作者

ovasty

发布于

2021-01-18

更新于

2021-12-28

许可协议

评论

You forgot to set the shortname for Disqus. Please set it in _config.yml.