vector组函数和鸡肋的as.vector()



vector(mode = "logical", length = 0)
as.vector(x,  mode = "any" #下面没写明默认值,所以Usage这里写的就算默认值
is.vector(x, mode = "any")
这三个函数都是同族的
vector produces a vector of the given length and mode.
vector()产生一个指定模式和长度的向量 

as.vector, a generic(泛型), attempts to coerce its argument into a vector of mode mode (the default is to coerce to whichever vector mode is most convenient): if the result is atomic all attributes are removed(结果是原生类型,则所有的属性将被移除).
as.vector()转化为一个指定模式的向量 

is.vector returns TRUE if x is a vector of the specified mode having no attributes other than names. It returns FALSE otherwise.
如果x是一种只含有names属性的特定模式类型的向量,则返回T(所以判断的时候要加上类型啊~( mode = "any")
判断是否为指定模式的向量(默认的模式是“any”)

Arguments
mode   
character string naming an  atomic mode(应指数值,字符,逻辑等类型 下面detail有解释) or "list" or "expression" or (except for vector) "any".
length   
a non-negative integer specifying the desired length. For a long vector, i.e., length > .Machine$integer.max, it has to be of type "double". Supplying an argument of length other than one is an error.
x   
an R object.

Details
The atomic modes are "logical", "integer", "numeric" (synonym(同义词) "double"), "complex", "character" and "raw".
 
If mode = "any", is.vector may return TRUE for the atomic modes, list and expression. For any mode, it will return FALSE if x has any attributes except names. (This is incompatible with S.) On the other hand, as.vector removes all attributes including names for results of atomic mode (but not those of mode "list" nor "expression").
如果mode是any,则 is.vector将会对原生模式,列表,表达式都返回T,只要含有除了names属性外的其他属性就返回F。
as.vector()将去除原生类型中的所有属性(包括names),但list和expression不会
 
Note that factors are not vectors; is.vector returns FALSE and  as.vector converts a factor to a character vector for mode = "any".
因子并不是向量,as.vector将其转化为字符向量
 
Value
For vector, a vector of the given length and mode. Logical vector elements are initialized to FALSE, numeric vector elements to 0, character vector elements to "", raw vector elements to nul bytes and list/expression elements to NULL.
对于给定长度和模式的向量。
逻辑向量用F初始化其元素,数值向量用0初始化其元素。
字符向量 用""初始化其元素, list/expression 用NULL初始化其元素

For is.vector, TRUE or FALSE.  is.vector(x, mode = "numeric") can be true for vectors of types "integer" or "double" whereas is.vector(x, mode = "double") can only be true for those of type "double".
使用is.vector的时候,最好指定mode(默认是nay),以便于更准确的判断

Methods for as.vector()
Writers of methods for as.vector need to take care to follow the conventions of the default method. In particular
Argument mode can be "any", any of the atomic modes, "list", "expression", "symbol", "pairlist" or one of the aliases(别名) "double" and "name".
The return value should be of the appropriate mode. For mode = "any" this means an atomic vector or list.
Attributes should be treated appropriately: in particular when the result is an atomic vector there should be no attributes, not even names.
is.vector(as.vector(x, m), m) should be true for any mode m, including the default "any".
 
Note
as.vector and is.vector are quite distinct from the meaning of the formal class "vector" in the methods package, and hence as(x, "vector") and is(x, "vector").
Note that as.vector(x) is not necessarily a null operation if is.vector(x) is true: any names will be removed from an atomic vector.
如果is.vector(x)的结果是T, as.vector(x)不一定是一个空操作(因为我虽然还是向量,但可以祛除names属性呀,所以我还是操作的),这里的x是同一个x

例子1
   
   
df <- data.frame(x = 1:3, y = 5:7)
   
   
try(as.vector(data.frame(x = 1:3, y = 5:7), mode = "numeric"))
Error in as.vector(data.frame(x = 1:3, y = 5:7), mode = "numeric") :
  (list) object cannot be coerced to type 'double'
因为数据框是列表的一种,列表不能转化为mode="numeric"?
试验
   
   
> as.vector(data.frame(x = 1:3, y = 5:7), mode = "list")
  x y
1 1 5
2 2 6
3 3 7
> class(as.vector(data.frame(x = 1:3, y = 5:7), mode = "list"))
[1] "data.frame"
> mode(as.vector(data.frame(x = 1:3, y = 5:7), mode = "list"))
[1] "list"
我自己再试了下
   
   
> test<-list(c(1,2),c(2,3))
> as.vector(test,mode="numeric")
Error in as.vector(test, mode = "numeric") : 
  (list) object cannot be coerced to type 'double'
得出结论,as.vector真是鸡肋
于是引出下一个问题:


例子2
   
   
> x <- c(a = 1, b = 2)
> is.vector(x)
[1] TRUE
> as.vector(x)
[1] 1 2
> all.equal(x, as.vector(x)) ## FALSE
[1] "names for target but not for current"
> attributes(x)
$names
[1] "a" "b"
 
> attributes(as.vector(x))
NULL
主要为了说明as.vector()会祛除names属性

例子3
   
   
> is.list(df)
[1] TRUE
> ! is.vector(df)
[1] TRUE
> is.vector(df, mode = "list")
[1] FALSE
> ! is.vector(df, mode = "list")
[1] TRUE
> is.vector(list(), mode = "list")
[1] TRUE



vector(mode = "logical", length = 0)
as.vector(x,  mode = "any" #下面没写明默认值,所以Usage这里写的就算默认值
is.vector(x, mode = "any")
这三个函数都是同族的
vector produces a vector of the given length and mode.
vector()产生一个指定模式和长度的向量 

as.vector, a generic(泛型), attempts to coerce its argument into a vector of mode mode (the default is to coerce to whichever vector mode is most convenient): if the result is atomic all attributes are removed(结果是原生类型,则所有的属性将被移除).
as.vector()转化为一个指定模式的向量 

is.vector returns TRUE if x is a vector of the specified mode having no attributes other than names. It returns FALSE otherwise.
如果x是一种只含有names属性的特定模式类型的向量,则返回T(所以判断的时候要加上类型啊~( mode = "any")
判断是否为指定模式的向量(默认的模式是“any”)

Arguments
mode   
character string naming an  atomic mode(应指数值,字符,逻辑等类型 下面detail有解释) or "list" or "expression" or (except for vector) "any".
length   
a non-negative integer specifying the desired length. For a long vector, i.e., length > .Machine$integer.max, it has to be of type "double". Supplying an argument of length other than one is an error.
x   
an R object.

Details
The atomic modes are "logical", "integer", "numeric" (synonym(同义词) "double"), "complex", "character" and "raw".
 
If mode = "any", is.vector may return TRUE for the atomic modes, list and expression. For any mode, it will return FALSE if x has any attributes except names. (This is incompatible with S.) On the other hand, as.vector removes all attributes including names for results of atomic mode (but not those of mode "list" nor "expression").
如果mode是any,则 is.vector将会对原生模式,列表,表达式都返回T,只要含有除了names属性外的其他属性就返回F。
as.vector()将去除原生类型中的所有属性(包括names),但list和expression不会
 
Note that factors are not vectors; is.vector returns FALSE and  as.vector converts a factor to a character vector for mode = "any".
因子并不是向量,as.vector将其转化为字符向量
 
Value
For vector, a vector of the given length and mode. Logical vector elements are initialized to FALSE, numeric vector elements to 0, character vector elements to "", raw vector elements to nul bytes and list/expression elements to NULL.
对于给定长度和模式的向量。
逻辑向量用F初始化其元素,数值向量用0初始化其元素。
字符向量 用""初始化其元素, list/expression 用NULL初始化其元素

For is.vector, TRUE or FALSE.  is.vector(x, mode = "numeric") can be true for vectors of types "integer" or "double" whereas is.vector(x, mode = "double") can only be true for those of type "double".
使用is.vector的时候,最好指定mode(默认是nay),以便于更准确的判断

Methods for as.vector()
Writers of methods for as.vector need to take care to follow the conventions of the default method. In particular
Argument mode can be "any", any of the atomic modes, "list", "expression", "symbol", "pairlist" or one of the aliases(别名) "double" and "name".
The return value should be of the appropriate mode. For mode = "any" this means an atomic vector or list.
Attributes should be treated appropriately: in particular when the result is an atomic vector there should be no attributes, not even names.
is.vector(as.vector(x, m), m) should be true for any mode m, including the default "any".
 
Note
as.vector and is.vector are quite distinct from the meaning of the formal class "vector" in the methods package, and hence as(x, "vector") and is(x, "vector").
Note that as.vector(x) is not necessarily a null operation if is.vector(x) is true: any names will be removed from an atomic vector.
如果is.vector(x)的结果是T, as.vector(x)不一定是一个空操作(因为我虽然还是向量,但可以祛除names属性呀,所以我还是操作的),这里的x是同一个x

例子1
    
    
df <- data.frame(x = 1:3, y = 5:7)
    
    
try(as.vector(data.frame(x = 1:3, y = 5:7), mode = "numeric"))
Error in as.vector(data.frame(x = 1:3, y = 5:7), mode = "numeric") :
  (list) object cannot be coerced to type 'double'
因为数据框是列表的一种,列表不能转化为mode="numeric"?
试验
    
    
> as.vector(data.frame(x = 1:3, y = 5:7), mode = "list")
  x y
1 1 5
2 2 6
3 3 7
> class(as.vector(data.frame(x = 1:3, y = 5:7), mode = "list"))
[1] "data.frame"
> mode(as.vector(data.frame(x = 1:3, y = 5:7), mode = "list"))
[1] "list"
我自己再试了下
    
    
> test<-list(c(1,2),c(2,3))
> as.vector(test,mode="numeric")
Error in as.vector(test, mode = "numeric") : 
  (list) object cannot be coerced to type 'double'
得出结论,as.vector真是鸡肋
于是引出下一个问题:


例子2
    
    
> x <- c(a = 1, b = 2)
> is.vector(x)
[1] TRUE
> as.vector(x)
[1] 1 2
> all.equal(x, as.vector(x)) ## FALSE
[1] "names for target but not for current"
> attributes(x)
$names
[1] "a" "b"
 
> attributes(as.vector(x))
NULL
主要为了说明as.vector()会祛除names属性

例子3
    
    
> is.list(df)
[1] TRUE
> ! is.vector(df)
[1] TRUE
> is.vector(df, mode = "list")
[1] FALSE
> ! is.vector(df, mode = "list")
[1] TRUE
> is.vector(list(), mode = "list")
[1] TRUE



  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值