需要注意的是,cbind() 和rbind() 是考虑dim 特性的连接函数,而函数c() 则不考虑这些数值对象的dim 和dimnames 属性。这一点在有些时候非常有用。将一个数组强制转换成简单向量的标准方法是用函数as.vector()。
vec <- as.vector(X) 一个相似效果的办法是采用单参数的c()函数,就像下面: vec <- c(X) 这两种方法有少许不同,到底采用那种方式关键是看对结果向量的格式要求(建议使用前者)。
c(base) c()所属R语言包:base Combine Values into a Vector or List 结合成一个向量或列表值 译者:生物统计家园网 机器人LoveR 描述----------Description---------- This is a generic function which combines its arguments. 这是一个通用的功能,结合其参数。 The default method combines its arguments to form a vector. All arguments are coerced to a common type which is the type of the returned value, and all attributes except names are removed. 默认的方法,结合它的参数,以形成一个向量。所有的参数被强制到一个共同的类型是返回值的类型,除名称外,所有属性将被删除。 用法----------Usage---------- c(…, recursive=FALSE) 参数----------Arguments---------- 参数:… objects to be concatenated. 要连接的对象。 参数:recursive logical. If recursive = TRUE, the function recursively descends through lists (and pairlists) combining all their elements into a vector. 逻辑。如果recursive = TRUE,函数的递归下降通过结合成一个向量元素(和pairlists)的名单。 Details 详情----------Details---------- The output type is determined from the highest type of the components in the hierarchy NULL < raw < logical < integer < real < complex < character < list < expression. Pairlists are treated as lists, but non-vector components (such names and calls) are treated as one-element lists which cannot be unlisted even if recursive = TRUE. 输出类型决定从最高的组件层次结构中的NULL类型<原料<逻辑<整数<真正复杂的<字符<列表<表达。的pairlists名单,但被视为非向量组件(如姓名,电话)作为一个元素列表,而不能是未上市,甚至如果recursive = TRUE治疗。 c is sometimes used for its side effect of removing attributes except names, for example to turn an array into a vector. as.vector is a more intuitive way to do this, but also drops names. Note too that methods other than the default are not required to do this (and they will almost certainly preserve a class attribute). c有时其副作用,除了名称删除属性,例如变成一个向量数组。 as.vector是一个更直观的方式做到这一点,但也下降名称。太注意默认以外的方法,并不需要做到这一点(和他们几乎肯定会维持一个类的属性)。 This is a primitive function. 这是一种原始的功能。 值----------Value---------- NULL or an expression or a vector of an appropriate mode. (With no arguments the value is NULL.) NULL或适当的方式表达或向量。 (不带参数的值是NULL。) S4方法----------S4 methods---------- This function is S4 generic, but with argument list (x, …, recursive = FALSE). 此功能是S4通用的,但参数列表(x, …, recursive = FALSE)。 参考文献----------References---------- The New S Language. Wadsworth & Brooks/Cole. 参见----------See Also---------- unlist and as.vector to produce attribute-free vectors. unlist和as.vector属性的向量。 举例----------Examples---------- c(1,7:9) c(1:5, 10.5, “next”)
x <- 1:4 names(x) <- letters[1:4] x c(x) # has names[有名字] as.vector(x) # no names[没有名字] dim(x) <- c(2,2) x c(x) as.vector(x)
ll <- list(A = 1, c=“C”)
c(ll, d = 1:3) # which is == c(ll, as.list(c(d=1:3))[这是== C(LL,as.list(C(D = 1:3))]
c(ll, d = list(1:3))# c() combining two lists[C()相结合的两个列表] c(list(A=c(B=1)), recursive=TRUE) c(options(), recursive=TRUE) c(list(A=c(B=1,C=2), B=c(E=7)), recursive=TRUE) 转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
http://www.biostatistic.net/thread-2396-1-1.html