R中的函数


总结R中使用过的函数的语法。

A

函数:append

Usage

append(x, values, after = length(x))

Arguments 参数
  • x
  • values
  • after
说明
Examples
1
2
3
# NOT RUN {
append(1:5, 0:1, after = 3)
# }

官网简介


函数:as.POSIX*

Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
as.POSIXct(x, tz = "", …)
as.POSIXlt(x, tz = "", …)
# S3 method for character
as.POSIXlt(x, tz = "", format,
tryFormats = c("%Y-%m-%d %H:%M:%OS",
"%Y/%m/%d %H:%M:%OS",
"%Y-%m-%d %H:%M",
"%Y/%m/%d %H:%M",
"%Y-%m-%d",
"%Y/%m/%d"),
optional = FALSE, …)
# S3 method for default
as.POSIXlt(x, tz = "",
optional = FALSE, …)
# S3 method for numeric
as.POSIXlt(x, tz = "", origin, …)

# S3 method for POSIXlt
as.double(x, …)
Arguments 参数
  • x 用来转换的R对象
  • tz 如果需要,则进行时区规范化。系统特定(参见时区),但””是当前时区,“GMT”是UTC(世界时,协调)。 在某些带有警告的平台上,无效值通常被视为UTC。

  • … 要传递给其他方法或从其他方法传递的其他参数

  • format 字符串给出strptime使用的日期时间格式
  • tryFormats 如果fromat未指定,则使用此字符串作为向量进行尝试
  • optional 逻辑指示如果格式猜测不成功则返回NA(而不是发出错误信号)
  • origin
说明
Examples

官网简介


B

b1

C

函数:chartr

Usage

chartr(old, new, x)
tolower(x)
toupper(x)
casefold(x, upper = FALSE)

Arguments 参数
  • x 字符向量,或者可以通过as.character强制转换为字符的对象。
  • old 一个字符串用来指定要转换的字符。 如果提供长度为2或更大的字符向量,则第一个元素将与警告一起使用。
  • new 指定翻译的字符串
  • upper 累计值,转换为大写还是小写
说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
> x <- "MiXeD cAsE 123" 
> chartr ("iXs" , "why" , x) # w替换i,h替换X,y替换s
[1] "MwheD cAyE 123"
> chartr ("a-cX" , "D-Fw" , x) #
[1] "MiweD FAsE 123"
> tolower (x)
[1] "mixed case 123"
> toupper (x)
[1] "MIXED CASE 123"
x <- "MiXeD cAsE 123"
chartr("iXs", "why", x)
chartr("a-cX", "D-Fw", x)
tolower(x)
toupper(x)

## "Mixed Case" Capitalizing - toupper( every first letter of a word ) :

.simpleCap <- function(x) {
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1, 1)), substring(s, 2),
sep = "", collapse = " ")
}
.simpleCap("the quick red fox jumps over the lazy brown dog")
## -> [1] "The Quick Red Fox Jumps Over The Lazy Brown Dog"

## and the better, more sophisticated version:
capwords <- function(s, strict = FALSE) {
cap <- function(s) paste(toupper(substring(s, 1, 1)),
{s <- substring(s, 2); if(strict) tolower(s) else s},
sep = "", collapse = " " )
sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s)))
}
capwords(c("using AIC for model selection"))
## -> [1] "Using AIC For Model Selection"
capwords(c("using AIC", "for MODEL selection"), strict = TRUE)
## -> [1] "Using Aic" "For Model Selection"
## ^^^ ^^^^^
## 'bad' 'good'

## -- Very simple insecure crypto --
rot <- function(ch, k = 13) {
p0 <- function(...) paste(c(...), collapse = "")
A <- c(letters, LETTERS, " '")
I <- seq_len(k); chartr(p0(A), p0(c(A[-I], A[I])), ch)
}

pw <- "my secret pass phrase"
(crypw <- rot(pw, 13)) #-> you can send this off

## now ``decrypt'' :
rot(crypw, 54 - 13) # -> the original:
stopifnot(identical(pw, rot(crypw, 54 - 13)))
# }

官网简介


函数:CRS-class

Class “CRS” Of Coordinate Reference System Arguments
PROJ.4 projection system的接口类。
该类被定义为空存根用来接受sp包中的NA值。
如果rgdal包可用,则该类将允许空间数据与坐标参考系统相关联。
必须完全按照PROJ.4文档中的参数输入参数,特别是+=字符串中不能有任何空格,并且连续的此类字符串只能用空格分隔。
请注意,地理坐标只接受“+proj=longlat +ellps=WGS84”,必须指定(东边,北边); 对于最新版本的PROJ.4库,必须给出(或从给定的“+datum=”值内部扩展)“+ellps=”定义,并且应该设置为适当的值。

Usage

CRS(projargs, doCheckCRSArgs=TRUE)
identicalCRS(x,y)

Arguments 参数
  • projargs 字符串类型,表示投影参数;此参数必须完全按照PROJ.4文档输入; 如果投影未知,则使用as.character(NA),它可能会丢失或为零长度的空字符串,然后将设置为缺失值。
  • doCheckCRSArgs 默认为TRUE,包开发人员必须将其设置为FALSE,包括S4类定义中的CRS,以避免无法控制地加载rgdal命名空间
  • x 具有proj4string方法的对象,或者如果缺少y,则列出具有proj4string方法的对象
  • y 类Spatial的对象,或者有proj4string方法的对象
说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
CRS()
CRS("")
CRS(as.character(NA))
CRS("+proj=longlat +datum=WGS84")
if (require(rgdal)) {
print(CRSargs(CRS("+proj=longlat +datum=NAD27")))
print(CRSargs(CRS("+init=epsg:4267")))
print(CRSargs(CRS("+init=epsg:26978")))
print(CRSargs(CRS(paste("+proj=sterea +lat_0=52.15616055555555",
"+lon_0=5.38763888888889 +k=0.999908 +x_0=155000 +y_0=463000 +ellps=bessel",
" +towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +units=m"))))
print(CRSargs(CRS("+init=epsg:28992")))
}

官网简介


函数:crs

Usage
1
2
3
4
5
6
7
8
9
10
11
st_crs(x, ...)

"st_crs"(x, ...)

"st_crs"(x, ...)

st_crs(x) <- value

"st_crs"(x) <- value

"st_crs"(x) <- value
Arguments 参数
说明
Examples

官网简介


函数:cumsum

简介:
返回一个向量,其元素是参数元素的累积和,乘积,最小值或最大值。

Usage

cumsum(x)
cumprod(x)
cummax(x)
cummin(x)

Arguments 参数
  • x 数字或复数(不是cummin或cummax)对象,或可以强制转换为其中一种的对象。
说明
Examples
1
2
3
4
5
6
7
8
9
10
> cumsum(100)
[1] 100
> cumsum(1:10) #累加
[1] 1 3 6 10 15 21 28 36 45 55
> cumprod(1:10) #累乘
[1] 1 2 6 24 120 720 5040 40320 362880 3628800
> cummin(c(3:1, 2:0, 4:2)) #后面的数不能大于前一个,大于的按等于前一个处理。
[1] 3 2 1 1 1 0 0 0 0
> cummax(c(3:1, 2:0, 4:2)) #后面的数一定要大于前一个,小的按等于前一个处理。
[1] 3 3 3 3 3 3 4 4 4

官网简介


D

函数:difftime

可以用来将时间转化为R中两个时间之差的格式,以方便进行比较。

Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
time1 - time2
difftime(time1, time2, tz,
units = c("auto", "secs", "mins", "hours",
"days", "weeks"))

as.difftime(tim, format = "%X", units = "auto")

# S3 method for difftime
format(x, ...)
# S3 method for difftime
units(x)
# S3 method for difftime
units(x) <- value
# S3 method for difftime
as.double(x, units = "auto", ...)

## Group methods, notably for round(), signif(), floor(),
## ceiling(), trunc(), abs(); called directly, *not* as Math():
# S3 method for difftime
Math(x, …)
Arguments 参数
说明

官网简介


函数:dim

简介:
检测或设置一个对象的维度参数。

Usage

dim(x)
dim(x) <- value

Arguments 参数
  • x
    一个R对象,比如:矩阵、数组、数据框(data frame)
  • value
    For the default method, either NULL or a numeric vector, which is coerced to integer (by truncation).
    对于默认方法,可以是NULL或数值向量,它被强制转换为整数(通过截断)。
说明

维数为value的元素个数,每一维度的长度是value向量对应的分量的值。
比如(3,4,5)向量设置的含义就是,三维,有3行,有4列,有5层,共计60个元素。

Examples
1
2
3
> x <- 1:12 ; dim(x) <- c(3,4)
> x
> View(x)

结果:

1
2
3
4
5
6
7
8
9
10
11
m <- matrix(1:12, 3, 4)
dim(m) # 3 4

d <- data.frame(a = 1:3, b = 4:6, c = 7:9, d = 10:12)
dim(d) # 3 4

a <- array(1:24, dim = 2:4)
dim(a) # 2 3 4

v <- 1:12
dim(v) # NULL

官网简介


函数:dist2Line

计算点到线的距离
官网简介


函数:distHaversine

官网简介


函数:distm

Usage

distm(x, y, fun=distHaversine)

Arguments 参数
  • x 点的精度/纬度
  • y 同x,如果缺省,则使用x的值
  • fun 距离计算函数 distCosine, distHaversine, distVincenty等
说明

http://en.wikipedia.org/wiki/Great_circle_distance

Examples
1
2
3
4
5
6
7
8
9
xy <- rbind(c(0,0),c(90,90),c(10,10),c(-120,-45))
distm(xy)
xy2 <- rbind(c(0,0),c(10,-10))
distm(xy, xy2)

t1=c(103.994191,30.589908)
t2=c(103.994644,30.588221)
aa=rbind(t1=t1,t2=t2)
distm(aa)

官网简介


函数:dtw

官网简介


F

函数:fix

简介:
相当于可以调用工具对R对象的值进行修改,调用的工具和对象类型有关。

Usage

fix(x, …)

Arguments 参数
  • x
说明
Examples

官网简介


K

函数:kalmanFilter

官网简介


函数:Kinhom.Track

Usage

Kinhom.Track(X,timestamp,correction=c(“border”, “bord.modif”,”isotropic”,”translate”),q,sigma=c(“default”,”bw.diggle”,”bw.ppl”,” bw.scott”),…)

Arguments 参数
说明
Examples

官网简介


L

函数:LCSS

官网简介


N

函数:names

简介:
查看变量所具有的名称,或给变量修改元素名称

Usage

names(x)
names(x) <- value

Arguments 参数
  • x R对象
  • value 一个与x长度相同的字符向量,或NULL
    官网简介
说明

使用demo

Examples
1
2
3
4
5
6
7
8
9
10
11
z <- list(a = 1, b = "c", c = 1:3)
names(z)
# change just the name of the third element.
names(z)[3] <- "c2"
z

z <- 1:3
names(z)
## assign just one name
names(z)[2] <- "b"
z

官网简介


函数:Normal 正态

Usage

dnorm(x, mean = 0, sd = 1, log = FALSE)
pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean = 0, sd = 1)

Arguments 参数
  • x, q vector of quantiles.
  • p vector of probabilities.
  • n number of observations. If length(n) > 1, the length is taken to be the number required.
  • mean vector of means.
  • sd
  • log, log.p
  • lower.tail
说明

资料1
资料2
资料3

Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# NOT RUN {
require(graphics)

dnorm(0) == 1/sqrt(2*pi)
dnorm(1) == exp(-1/2)/sqrt(2*pi)
dnorm(1) == 1/sqrt(2*pi*exp(1))

## Using "log = TRUE" for an extended range :
par(mfrow = c(2,1))
plot(function(x) dnorm(x, log = TRUE), -60, 50,
main = "log { Normal density }")
curve(log(dnorm(x)), add = TRUE, col = "red", lwd = 2)
mtext("dnorm(x, log=TRUE)", adj = 0)
mtext("log(dnorm(x))", col = "red", adj = 1)

plot(function(x) pnorm(x, log.p = TRUE), -50, 10,
main = "log { Normal Cumulative }")
curve(log(pnorm(x)), add = TRUE, col = "red", lwd = 2)
mtext("pnorm(x, log=TRUE)", adj = 0)
mtext("log(pnorm(x))", col = "red", adj = 1)

## if you want the so-called 'error function'
erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1
## (see Abramowitz and Stegun 29.2.29)
## and the so-called 'complementary error function'
erfc <- function(x) 2 * pnorm(x * sqrt(2), lower = FALSE)
## and the inverses
erfinv <- function (x) qnorm((1 + x)/2)/sqrt(2)
erfcinv <- function (x) qnorm(x/2, lower = FALSE)/sqrt(2)
# }

官网简介


M

函数:merge

Usage

merge(x, y, …)
# S3 method for default
merge(x, y, …)
# S3 method for data.frame
merge(x, y, by = intersect(names(x), names(y)),
by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all,
sort = TRUE, suffixes = c(“.x”,”.y”), no.dups = TRUE,
incomparables = NULL, …)

Arguments 参数
  • x, y
  • by, by.x, by.y
  • all
  • all.x
  • all.y
  • sort
  • suffixes
  • no.dups
  • incomparables
说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# NOT RUN {
authors <- data.frame(
## I(*) : use character columns of names to get sensible sort order
surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
nationality = c("US", "Australia", "US", "UK", "Australia"),
deceased = c("yes", rep("no", 4)))
authorN <- within(authors, { name <- surname; rm(surname) })
books <- data.frame(
name = I(c("Tukey", "Venables", "Tierney",
"Ripley", "Ripley", "McNeil", "R Core")),
title = c("Exploratory Data Analysis",
"Modern Applied Statistics ...",
"LISP-STAT",
"Spatial Statistics", "Stochastic Simulation",
"Interactive Data Analysis",
"An Introduction to R"),
other.author = c(NA, "Ripley", NA, NA, NA, NA,
"Venables & Smith"))

(m0 <- merge(authorN, books))
(m1 <- merge(authors, books, by.x = "surname", by.y = "name"))
m2 <- merge(books, authors, by.x = "name", by.y = "surname")
stopifnot(exprs = {
identical(m0, m2[, names(m0)])
as.character(m1[, 1]) == as.character(m2[, 1])
all.equal(m1[, -1], m2[, -1][ names(m1)[-1] ])
identical(dim(merge(m1, m2, by = NULL)),
c(nrow(m1)*nrow(m2), ncol(m1)+ncol(m2)))
})

## "R core" is missing from authors and appears only here :
merge(authors, books, by.x = "surname", by.y = "name", all = TRUE)


## example of using 'incomparables'
x <- data.frame(k1 = c(NA,NA,3,4,5), k2 = c(1,NA,NA,4,5), data = 1:5)
y <- data.frame(k1 = c(NA,2,NA,4,5), k2 = c(NA,NA,3,4,5), data = 1:5)
merge(x, y, by = c("k1","k2")) # NA's match
merge(x, y, by = "k1") # NA's match, so 6 rows
merge(x, y, by = "k2", incomparables = NA) # 2 rows
# }

官网简介


O

函数:outer

简介:
The outer product of the arrays X and Y is the array A with dimension c(dim(X), dim(Y)) where element A[c(arrayindex.x, arrayindex.y)] = FUN(X[arrayindex.x], Y[arrayindex.y], …).
数组X和Y的外积是数组A,则数组A的维度是c(dim(X), dim(Y))
元素 A[c(arrayindex.x, arrayindex.y)] = FUN(X[arrayindex.x], Y[arrayindex.y], …)

Usage

outer(X, Y, FUN = “*”, …)
X %o% Y

Arguments 参数
  • X, Y
  • FUN
说明

X %o% Y :

Examples
1
2
3
4
5
6
>test1 <- 1:9
>test2 <- 1:2
> outer(test2, test1, "^")
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 1 1 1 1 1 1 1 1
[2,] 2 4 8 16 32 64 128 256 512

官网简介


P

函数:par

Usage

par(…, no.readonly = FALSE)
(…, = )

Arguments 参数
  • no.readonly
说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# NOT RUN {
op <- par(mfrow = c(2, 2), # 2 x 2 pictures on one plot
pty = "s") # square plotting region,
# independent of device size

## At end of plotting, reset to previous settings:
par(op)

## Alternatively,
op <- par(no.readonly = TRUE) # the whole list of settable par's.
## do lots of plotting and par(.) calls, then reset:
par(op)
## Note this is not in general good practice

par("ylog") # FALSE
plot(1 : 12, log = "y")
par("ylog") # TRUE

plot(1:2, xaxs = "i") # 'inner axis' w/o extra space
par(c("usr", "xaxp"))

( nr.prof <-
c(prof.pilots = 16, lawyers = 11, farmers = 10, salesmen = 9, physicians = 9,
mechanics = 6, policemen = 6, managers = 6, engineers = 5, teachers = 4,
housewives = 3, students = 3, armed.forces = 1))
par(las = 3)
barplot(rbind(nr.prof)) # R 0.63.2: shows alignment problem
par(las = 0) # reset to default

require(grDevices) # for gray
## 'fg' use:
plot(1:12, type = "b", main = "'fg' : axes, ticks and box in gray",
fg = gray(0.7), bty = "7" , sub = R.version.string)

ex <- function() {
old.par <- par(no.readonly = TRUE) # all par settings which
# could be changed.
on.exit(par(old.par))
## ...
## ... do lots of par() settings and plots
## ...
invisible() #-- now, par(old.par) will be executed
}
ex()

## Line types
showLty <- function(ltys, xoff = 0, ...) {
stopifnot((n <- length(ltys)) >= 1)
op <- par(mar = rep(.5,4)); on.exit(par(op))
plot(0:1, 0:1, type = "n", axes = FALSE, ann = FALSE)
y <- (n:1)/(n+1)
clty <- as.character(ltys)
mytext <- function(x, y, txt)
text(x, y, txt, adj = c(0, -.3), cex = 0.8, ...)
abline(h = y, lty = ltys, ...); mytext(xoff, y, clty)
y <- y - 1/(3*(n+1))
abline(h = y, lty = ltys, lwd = 2, ...)
mytext(1/8+xoff, y, paste(clty," lwd = 2"))
}
showLty(c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash"))
par(new = TRUE) # the same:
showLty(c("solid", "44", "13", "1343", "73", "2262"), xoff = .2, col = 2)
showLty(c("11", "22", "33", "44", "12", "13", "14", "21", "31"))
# }

官网简介


函数:paste

difference-between-paste-and-paste0

Usage

paste (…, sep = “ “, collapse = NULL)
paste0(…, collapse = NULL)

Arguments 参数

  • one or more R objects, to be converted to character vectors.
    字符向量,用“,”分隔
  • sep
    a character string to separate the terms. Not NA_character_.
    用于分割的字符串
  • collapse
    an optional character string to separate the results. Not NA_character_.
    连接符
说明

会将传入的n个向量进行连接,连接次数以向量中分量最多的向量为准,其它向量则循环使用。
sep用来分隔(连接)分量,collapse用来将连接后的分量进行连接为一个字符串。
比如一个向量为(a,b,c,d),另一个向量为(1,2,3),还有(1)。
则连接次数为4。
(1,2,3)则循环为:1,2,3,1
(1)则循环为:1,1,1,1
如果不设置参数,则为:

1
2
> paste(c('a','b','c','d'),c(1,2,3),1)
[1] "a 1 1" "b 2 1" "c 3 1" "d 1 1"

如果设置参数,比如sep=‘_’,则:

1
2
> paste(c('a','b','c','d'),c(1,2,3),1,sep = '_')
[1] "a_1_1" "b_2_1" "c_3_1" "d_1_1"

如果设置参数,比如sep=‘_’,collapse=’&’则:

1
2
> paste(c('a','b','c','d'),c(1,2,3),1,sep = '_',collapse='&')
[1] "a_1_1&b_2_1&c_3_1&d_1_1"

Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> paste('one',2,'three',4,'five')
[1] "one 2 three 4 five"
> paste('X',1:5,sep='')
[1] "X1" "X2" "X3" "X4" "X5"
> paste(c('one','two','three','four'),collapse=' and ')
[1] "one and two and three and four"
> paste(c('X','Y'),1:5,sep='_',collapse=' and ')
[1] "X_1 and Y_2 and X_3 and Y_4 and X_5"
> paste(1:5,6:9)
[1] "1 6" "2 7" "3 8" "4 9" "5 6"
> paste(1:5,6:9,10:11)
[1] "1 6 10" "2 7 11" "3 8 10" "4 9 11" "5 6 10"
> paste(1:2,6:9,10:11)
[1] "1 6 10" "2 7 11" "1 8 10" "2 9 11"
> paste(1:2,6:9,sep='_',collapse=' and ')
[1] "1_6 and 2_7 and 1_8 and 2_9"
> paste(1:2,6:9,10:11,sep='_',collapse=' and ')
[1] "1_6_10 and 2_7_11 and 1_8_10 and 2_9_11"

官网简介


函数:plot

画图函数

Usage

plot(x, y, …)

Arguments 参数
  • x plot中点的坐标。或者,任何可以提供单个绘图结构,函数或具有绘图方法的R对象。

  • y plot中点的y坐标,如果x是合适的结构(比如包含了完善的位置信息),则可以没有y。

  • … 传递给方法的参数,比如 graphical parameters (see par)。许多方法接受以下参数:

    • type plot应该画什么类型的图形,可能是以下图形中的一种:

      • "p"
      • "l" 线
      • "b" for both,
      • "c" for the lines part alone of "b",
      • "o" for both ‘overplotted’,
      • "h" 直方图 like (or ‘high-density’) vertical lines,
      • "s" 阶梯图
      • "S" for other steps, see ‘Details’ below,
      • "n" for no plotting.
    • main 画出的图的标题: see title.

    • sub 子标题: see title.
    • xlab x轴标题: see title.
    • ylab y轴标题: see title.
    • asp y / x纵横比, see plot.window.
说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# NOT RUN {
require(stats) # for lowess, rpois, rnorm
plot(cars)
lines(lowess(cars))

plot(sin, -pi, 2*pi) # see ?plot.function

## Discrete Distribution Plot:
plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
main = "rpois(100, lambda = 5)")

## Simple quantiles/ECDF, see ecdf() {library(stats)} for a better one:
plot(x <- sort(rnorm(47)), type = "s", main = "plot(x, type = \"s\")")
points(x, cex = .5, col = "dark red")
# }

plot(1:100, (1:100) ^ 2, main = "plot(1:100, (1:100) ^ 2)")

plot(
1:25,
cex = 3,
lwd = 3,
pch = 1:25,
col = rainbow(25),
bg = c(rep(NA, 20), terrain.colors(5)),
main = "plot(1:25, pch = 1:25, ...)"
)

plot(
(1:100) ^ 2,
type = "l",
main = 'plot((1:100) ^ 2, type = "l")'
)

plot(
(1:100) ^ 2,
type = "l",
lty = "dashed",
lwd = 3,
col = "chocolate",
main = 'plot((1:100) ^ 2, type = "l", lty = "dashed", ...)'
)

with(
cars,
plot(speed, dist, main = "with(cars, plot(speed, dist))")
)

官网案例挺多的

官网简介
参考资料1–R语言plot函数参数合集


Q

函数:q1

Usage

rep(x, …)
rep.int(x, times)
rep_len(x, length.out)

Arguments 参数
  • x
    a vector (of any mode including a list) or a factor or (for rep only) a POSIXct or POSIXlt or Date object; or an S4 object containing such an object.

  • further arguments to be passed to or from other methods. For the internal default method these can include:
    times
    an integer-valued vector giving the (non-negative) number of times to repeat each element if of length length(x), or to repeat the whole vector if of length 1. Negative or NA values are an error. A double vector is accepted, other inputs being coerced to an integer or double vector.
    length.out
    non-negative integer. The desired length of the output vector. Other inputs will be coerced to a double vector and the first element taken. Ignored if NA or invalid.
    each
    non-negative integer. Each element of x is repeated each times. Other inputs will be coerced to an integer or double vector and the first element taken. Treated as 1 if NA or invalid.
  • times, length.out
    see … above
说明
Examples

官网简介

R

函数:rep

Usage

rep(x, …)
rep.int(x, times)
rep_len(x, length.out)

Arguments 参数
  • x

  • times:each处理后的重复次数
    len:总分量个数
    each:每个重复多少次
  • times, length.out
说明

依次把值赋值给 times -> len -> each
作用的优先级是 len > times > each
即先each生成一个向量,然后times处理后生成一个向量,最后len处理得到最终的向量

Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
rep(2:5, 2) 
rep(2:5, 2,2,2,2)

rep(2:5, 2,2,2)
# 依次把值赋值给 times -> len -> each
# 作用的优先级是 len > times > each
# 即先each生成一个向量,然后times处理后生成一个向量,最后len处理得到最终的向量


# NOT RUN {
rep(1:4, 2)
rep(1:4, each = 2, len = 4) # not the same.
rep(1:4, c(2,2,2,2), len = 4) # same as second.


rep(1:4, c(2,1,2,1), len = 4)

rep(1:4, each = 2, len = 4) # first 4 only.
rep(1:4, each = 2, len = 10) # 8 integers plus two recycled 1's.
rep(1:4, each = 2, times = 3) # length 24, 3 complete replications
rep(1:4, each = 2, times = 3,len =10) # length 24, 3 complete replications

rep(1, 40*(1-.8)) # length 7 on most platforms
rep(1, 40*(1-.8)+1e-7) # better

## replicate a list
fred <- list(happy = 1:10, name = "squash")
rep(fred, 5)

# date-time objects
x <- .leap.seconds[1:3]
rep(x, 2)
rep(as.POSIXlt(x), rep(2, 3))

## named factor
x <- factor(LETTERS[1:4]); names(x) <- letters[1:4]
x
rep(x, 2)
rep(x, each = 2)
rep.int(x, 2) # no names
rep_len(x, 10)
# }

官网简介


函数:rnorm

Usage

rnorm.acomp(n,mean,var)
rnorm.rcomp(n,mean,var)
rnorm.aplus(n,mean,var)
rnorm.rplus(n,mean,var)
rnorm.rmult(n,mean,var)
rnorm.ccomp(n,mean,var,lambda)
dnorm.acomp(x,mean,var)
dnorm.aplus(x,mean,var)
dnorm.rmult(x,mean,var)

Arguments 参数
  • n 要模拟的数据集数量

  • mean 要模拟的数据集的平均值

  • var 方差协方差矩阵

  • lambda 预计总数

  • x 采样空间中的向量

说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# NOT RUN {
MyVar <- matrix(c(
0.2,0.1,0.0,
0.1,0.2,0.0,
0.0,0.0,0.2),byrow=TRUE,nrow=3)
MyMean <- c(1,1,2)

plot(rnorm.acomp(100,MyMean,MyVar))
plot(rnorm.rcomp(100,MyMean,MyVar))
plot(rnorm.aplus(100,MyMean,MyVar))
plot(rnorm.rplus(100,MyMean,MyVar))
plot(rnorm.rmult(100,MyMean,MyVar))
x <- rnorm.aplus(5,MyMean,MyVar)
dnorm.acomp(x,MyMean,MyVar)
dnorm.aplus(x,MyMean,MyVar)
dnorm.rmult(x,MyMean,MyVar)
# }

官网简介


S

函数:sample

Usage

sample(x, size, replace = FALSE, prob = NULL)
sample.int(n, size = n, replace = FALSE, prob = NULL,
useHash = (!replace && is.null(prob) && size <= 2=”” n=”” &&=””> 1e7))

Arguments 参数
  • x :提供的采样范围、训练集
  • n :最终需要
  • size :总计需要抽取多少个样本
  • replace :是否放回,如果是,则抽出的结果可能有重复的,反之,则没有
  • prob :概率
  • useHash :
说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# NOT RUN {
x <- 1:12
# a random permutation
sample(x)
# bootstrap resampling -- only if length(x) > 1 !
sample(x, replace = TRUE) #相当于有重复的组合,用的是x中的元素

# 100 Bernoulli trials
sample(c(0,1), 100, replace = TRUE)

## More careful bootstrapping -- Consider this when using sample()
## programmatically (i.e., in your function or simulation)!

# sample()'s surprise -- example
x <- 1:10
sample(x[x > 8]) # length 2
sample(x[x > 9]) # oops -- length 10!
sample(x[x > 10]) # length 0

## safer version:
resample <- function(x, ...) x[sample.int(length(x), ...)]
resample(x[x > 8]) # length 2
resample(x[x > 9]) # length 1
resample(x[x > 10]) # length 0

## R 3.x.y only
sample.int(1e10, 12, replace = TRUE)
sample.int(1e10, 12) # not that there is much chance of duplicates
# }

官网简介


函数:scale

Usage

scale(x, center = TRUE, scale = TRUE)

Arguments 参数
  • x 数字矩阵
  • center 长度等于x的列数的逻辑值或类似数字的向量,其中’numeric-alike’表示如果is.numeric()不为true,即不为数字,则as.numeric()将应用,即强制转换为数字。
  • scale 长度等于x的列数的逻辑值或类似数字的向量
说明

在我们做数据的时候,一个数据会有很多特征;比如在描述影响房价的因素,有房子面积,房间数量等。而不同的特征存在不同的量纲,为了消除量纲、数值差异等,我们就需要对数据进行中心化和标准化;

那什么是中心化,什么是标准化呢?
所谓中心化就是将数据减去均值后得到的,比如有一组数据(1,2,3,4,5,6,7),它的均值是4,中心化后的数据为(-3,-2,-1,0,1,2,3)
而标准化则是在中心化后的数据基础上再除以数据的标准差
在R语言中可以通过scale函数直接进行数据的中心化和标准化,具体如下:

Scale(x,center,scale)
参数解释:x—即需要标准化的数据
center—表示是否进行中心化
scale—表示是否进行标准化

1、数据的中心化
所谓数据的中心化是指数据集中的各项数据减去数据集的均值。
例如有数据集1, 2, 3, 6, 3,其均值为3
那么中心化之后的数据集为1-3,2-3,3-3,6-3,3-3,即:-2,-1,0,3,0

2、数据的标准化
所谓数据的标准化是指中心化之后的数据在除以数据集的标准差,即数据集中的各项数据减去数据集的均值再除以数据集的标准差。
例如有数据集1, 2, 3, 6, 3,其均值为3,其标准差为1.87
那么标准化之后的数据集为(1-3)/1.87,(2-3)/1.87,(3-3)/1.87,(6-3)/1.87,(3-3)/1.87,即:-1.069,-0.535,0,1.604,0

Examples

官网简介
参考资料1
参考资料2


函数:seq

参考资料1
参考资料2


函数:sink

说明
Examples

简介:
sink将R的输出转移到连接(并在之后使用sink停止这种转移)
sink.number() 显示有多少个正在使用的连接数
sink.number(type = “message”) 报告当前用于错误消息的连接数

Usage

sink(file = NULL, append = FALSE, type = c(“output”, “message”),split = FALSE)
sink.number(type = c(“output”, “message”))

Arguments 参数
  • file a writable connection or a character string naming the file to write to, or NULL to stop sink-ing.
    一个可写的连接或要写入的文件的文件名或NULL(用来停止sink)
  • append
    值为TRUE/FALSE,如果为TRUE,输出会追加到文件;否则,重写文件,不保留之前的内容(如果有的话)
  • type
    字符串类型,输出流或消息流,名称将部分匹配,因此可以缩写。
  • split
    值为TRUE/FALSE,如果为TRUE,输出将被发送到新的sink和当前的输出流,就像Unix程序tee一样。
说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# NOT RUN {
sink("sink-examp.txt")
i <- 1:10
outer(i, i, "*")
sink()
# }
# NOT RUN {

# }
# NOT RUN {
## capture all the output to a file.
zz <- file("all.Rout", open = "wt")
sink(zz)
sink(zz, type = "message") #会将日志写入输出流中去
try(log("a"))
## revert output back to the console -- only then access the file!
sink(type = "message")
sink()
file.show("all.Rout")
# }

官网简介


函数: stplot

Usage
Arguments 参数
说明
Examples

官网简介



T

函数:tracks

Usage
Arguments 参数
说明
Examples

官网简介


U

函数:unlist

Usage

unlist(x, recursive = TRUE, use.names = TRUE)

Arguments 参数
  • x R对象,通常是列表或向量
  • recursive 逻辑值 是否应该将非列表化应用于x的列表组件
  • use.names 逻辑值 名字是否保留
说明
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# NOT RUN {
unlist(options())
unlist(options(), use.names = FALSE)

l.ex <- list(a = list(1:5, LETTERS[1:5]), b = "Z", c = NA)
unlist(l.ex, recursive = FALSE)
unlist(l.ex, recursive = TRUE)

l1 <- list(a = "a", b = 2, c = pi+2i)
unlist(l1) # a character vector
l2 <- list(a = "a", b = as.name("b"), c = pi+2i)
unlist(l2) # remains a list

ll <- list(as.name("sinc"), quote( a + b ), 1:10, letters, expression(1+x))
utils::str(ll)
for(x in ll)
stopifnot(identical(x, unlist(x)))
# }

官网简介