R in Action notes

chapter02

向量:一维数组(存数值型、字符型、逻辑型数据)

a <- c(2:8)

a[2:5]

 

矩阵:二维数组

A matrix is a two-dimensional array where each element has the same mode (numeric,
character, or logical). Matrices are created with the matrix function. The general for-
mat is
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,
byrow=logical_value, dimnames=list(
char_vector_rownames, char_vector_colnames))

code sample:

 

cells <- c(1, 26, 24, 68)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow = 2, ncol = 2, byrow = TRUE,
dimnames = list(rnames, cnames))
mymatrix
mymatrix <- matrix(cells, nrow = 2, ncol = 2, byrow = FALSE,
dimnames = list(rnames, cnames))

x <- matrix(1:10, nrow = 2)

x
x[2, ]
x[, 2]
x[1, 4]
x[1, c(4, 5)]

 

数组:与矩阵类似,但是维度可以大于2。

Arrays are similar to matrices but can have more than two dimensions. They’re created
with an array function of the following form:
myarray <- array(vector, dimensions, dimnames)
where vector contains the data for the array, dimensions is a numeric vector giving
the maximal index for each dimension, and dimnames is an optional list of dimension
labels.

 

数据框:不同的列可以包含不同的模式

A data frame is more general than a matrix in that different columns can contain
different modes of data (numeric, character, etc.).

A data frame is created with the data.frame() function:
mydata <- data.frame(col1, col2, col3,...)

 

attach(),detach(),with()

因子:变量可归结为名义型、有序型或连续型,类别(名义型)变量和有序类别变量在R中成为因子(factor)。

Categorical (nominal) and ordered categorical (ordinal) variables in R are called
factors.

 

You can override the default by specifying a levels option. For example,
status <- factor(status, order=TRUE,
levels=c("Poor", "Improved", "Excellent"))

 

 

列表:列表就是一些对象的有序集合

 

you create a list using the list() function:
mylist <- list(object1, object2, ...)
where the objects are any of the structures seen so far. Optionally, you can name the
objects in a list:
mylist <- list(name1=object1, name2=object2, ...)

 

code samples:

 

 

g <- "My First List"
h <- c(25, 26, 18, 39)
j <- matrix(1:10, nrow = 5)
k <- c("one", "two", "three")
mylist <- list(title = g, ages = h, j, k)
mylist

 

 

chapter 03

simple sample:

 

ose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type="b")

 

Listing 3.1
Using graphical parameters to control graph appearance
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly=TRUE)
par(pin=c(2, 3))
par(lwd=2, cex=1.5)
par(cex.axis=.75, font.axis=3)
plot(dose, drugA, type="b", pch=19, lty=2, col="red")
plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green")
par(opar)

title(main="main title", sub="sub-title",
xlab="x-axis label", ylab="y-axis label")

 

 

posted on 2013-06-19 18:25  ukouryou  阅读(167)  评论(0编辑  收藏  举报

导航