> a <-c(1,2,5,3,6,-2,4) > b <-c("one","two","three") >c<-c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) > a [1]12536-24 > b [1]"one""two""three" >c [1]TRUETRUETRUEFALSETRUEFALSE
as.matrix(x, ...) ## S3 method for class 'data.frame' as.matrix(x, rownames.force =NA, ...)
is.matrix(x)
参数
描述
data
包含矩阵的元素 an optional data vector (including a list or expression vector). Non-atomic classed R objects are coerced by as.vector and all attributes discarded.
nrow
指定行的维数(指定目标矩阵有多少行) the desired number of rows.
ncol
指定列的维数(指定目标矩阵有多少列) the desired number of columns.
byrow
logical. 按行填充(byrow=TRUE); 按列填充(byrow=FALSE) If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.
dimnames
包含了可选的、以字符型向量表示的行名和列名 A dimnames attribute for the matrix: NULL or a list of length 2 giving the row and column names respectively. An empty list is treated as NULL, and a list of length one as row names. The list can be named, and the list names will be used as names for the dimensions. 使用方式为:dimnames=list(rnames, cnames)
x
an R object.
例1,创建一个5×4的矩阵:
1 2 3 4 5 6 7 8
> y <- matrix(1:20, nrow=5, ncol=4) > y [,1][,2][,3][,4] [1,]161116 [2,]271217 [3,]381318 [4,]491419 [5,]5101520
数组中的数据 a vector (including a list or expression vector) giving data to fill the array. Non-atomic classed objects are coerced by as.vector.
dim
是一个数值型向量,给出了各个维度下标的最大值 the dim attribute for the array to be created, that is an integer vector of length one or more giving the maximal indices in each dimension.
dimnames
可选参数、各维度名称标签的列表 either NULL or the names for the dimensions. This must a list (or it will be ignored) with one component for each dimension, either NULL or a character vector of the length given by dim for that dimension. The list can be named, and the list names will be used as names for the dimensions. If the list is shorter than the number of dimensions, it is extended by NULLs to the length required.
default.stringsAsFactors()# << this is deprecated !
参数
描述
…
列向量,可为任何类型(如字符型、数值型或逻辑型) these arguments are of either the form value or tag = value. Component names are created based on the tag (if present) or the deparsed argument itself.
row.names
NULL or a single integer or character string specifying a column to be used as row names, or a character or integer vector giving the row names for the data frame.
check.rows
if TRUE then the rows are checked for consistency of length and names.
check.names
logical. If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names and are not duplicated. If necessary they are adjusted (by make.names) so that they are.
fix.empty.names
logical indicating if arguments which are “unnamed” (in the sense of not being formally called as someName = arg) get an automatically constructed name or rather name “”. Needs to be set to FALSE even when check.names is false if “” names should be kept.
stringsAsFactors
logical: should character vectors be converted to factors? The ‘factory-fresh’ default has been TRUE previously but has been changed to FALSE for R 4.0.0. Only as short time workaround, you can revert by setting options(stringsAsFactors = TRUE) which now warns about its deprecation.
# 修改第一列名称 >names(patientdata)[1]<-"Patients ID Number" > patientdata Patients ID Number age diabetes status 1125 Type1 Poor 2234 Type2 Improved 3328 Type1 Excellent 4452 Type1 Poor
索引
选取数据框中元素的方式有若干种。你可以使用前述(如矩阵中的)下标记号,亦可直接指定列名。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 直接使用下标记号 > patientdata[1:2] patientID age 1125 2234 3328 4452
# 显示对象的结构 > str(patientdata) 'data.frame':4 obs. of 4 variables: $ patientID: num 1234 $ age : num 25342852 $ diabetes : chr "Type1""Type2""Type1""Type1" $ status : chr "Poor""Improved""Excellent""Poor"
# 显示对象的统计概要 > summary(patientdata) patientID age diabetes status Min. :1.00 Min. :25.00 Length:4 Length:4 1st Qu.:1.751st Qu.:27.25 Class :character Class :character Median :2.50 Median :31.00 Mode :character Mode :character Mean :2.50 Mean :34.75 3rd Qu.:3.253rd Qu.:38.50 Max. :4.00 Max. :52.00
# 以向量形式输入数据 > patientID <-c(1,2,3,4) > age <-c(25,34,28,52) > diabetes <-c("Type1","Type2","Type1","Type1") > status <-c("Poor","Improved","Excellent","Poor") > diabetes <- factor(diabetes) > status <- factor(status, order=TRUE) > patientdata <- data.frame(patientID, age, diabetes, status)
# 显示对象的结构 > str(patientdata) 'data.frame':4 obs. of 4 variables: $ patientID: num 1234 $ age : num 25342852 $ diabetes : Factor w/2 levels "Type1","Type2":1211 $ status : Ord.factor w/3 levels "Excellent"<"Improved"<..:3213
# 显示对象的统计概要 > summary(patientdata) patientID age diabetes status Min. :1.00 Min. :25.00 Type1:3 Excellent:1 1st Qu.:1.751st Qu.:27.25 Type2:1 Improved :1 Median :2.50 Median :31.00 Poor :2 Mean :2.50 Mean :34.75 3rd Qu.:3.253rd Qu.:38.50 Max. :4.00 Max. :52.00
as.list(x, ...) ## S3 method for class 'environment' as.list(x, all.names =FALSE, sorted =FALSE, ...) as.pairlist(x)
is.list(x) is.pairlist(x)
alist(...)
参数
描述
…
objects, possibly named.
x
object to be coerced or tested.
all.names
a logical indicating whether to copy all values or (default) only those whose names do not begin with a dot.
sorted
a logical indicating whether the names of the resulting list should be sorted (increasingly). Note that this is somewhat costly, but may be useful for comparison of environments.