本文原创,转载请注明出处,本人Q1273314690
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
  1. df <- data.frame(x =1:3, y =5:7)
  1. try(as.vector(data.frame(x =1:3, y =5:7), mode ="numeric"))
  2. Error in as.vector(data.frame(x =1:3, y =5:7), mode ="numeric"):
  3.   (list) object cannot be coerced to type 'double'
因为数据框是列表的一种,列表不能转化为mode="numeric"?
试验
  1. > as.vector(data.frame(x =1:3, y =5:7), mode ="list")
  2.   x y
  3. 115
  4. 226
  5. 337
  6. >class(as.vector(data.frame(x =1:3, y =5:7), mode ="list"))
  7. [1]"data.frame"
  8. > mode(as.vector(data.frame(x =1:3, y =5:7), mode ="list"))
  9. [1]"list"
我自己再试了下
  1. > test<-list(c(1,2),c(2,3))
  2. > as.vector(test,mode="numeric")
  3. Error in as.vector(test, mode ="numeric"): 
  4.   (list) object cannot be coerced to type 'double'
得出结论,as.vector真是鸡肋
//2016年3月18日22:32:15
然而,你并不能这么说它鸡肋,他是可以成功将矩阵转为向量的,只是不能对数据框罢了,因为数据本就是存不同类型的数据的,你并不能强求人家转,而且这也没什么意义。
于是引出下一个问题:
 
 
例子2
  1. > x <- c(a =1, b =2)
  2. > is.vector(x)
  3. [1] TRUE
  4. > as.vector(x)
  5. [1]12
  6.  
  7. > all.equal(x, as.vector(x))## FALSE
  8.  
  9. [1]"names for target but not for current"
  10.  
  11. > attributes(x)
  12. $names
  13. [1]"a""b"
  14.  
  15. > attributes(as.vector(x))
  16. NULL
主要为了说明as.vector()会祛除names属性
 
例子3
  1. > is.list(df)
  2. [1] TRUE
  3. >! is.vector(df)
  4. [1] TRUE
  5.  
  6. > is.vector(df, mode ="list")
  7. [1] FALSE
  8.  
  9. >! is.vector(df, mode ="list")
  10. [1] TRUE
  11.  
  12. > is.vector(list(), mode ="list")
  13. [1] TRUE
 
 
 
 
 

vector族函数的更多相关文章

  1. exec族函数详解及循环创建子进程

    前言:之前也知道exec族函数,但没有完全掌握,昨天又重新学习了一遍,基本完全掌握了,还有一些父子进程和循环创建子进程的问题,还要介绍一下环境变量,今天分享一下. 一.环境变量 先介绍下环境的概念和特 ...

  2. Linux学习笔记(8)-exec族函数

    昨天学习了Linux下的进程创建,创建一个进程的方法极为简单,只需要调用fork函数就可以创建出一个进程,但是-- 介绍fork()函数的时候提到,在创建进程后,子进程与父进程有相同的代码空间,执行的 ...

  3. R-- Apply族函数

    APPLY族函数: apply(x,a,f) 对矩阵或数据框的某一维度作用函数fx为矩阵或数据框:a为1代表行,a为2代表列:f为作用函数. lapply(x,f) 对x的每一个元组作用函数f,结果以 ...

  4. vector作为函数返回类型

    在实际的操作中,我们经常会碰到需要返回一序列字符串或者一列数字的时候,以前会用到数组来保存这列的字符串或者数字,现在我们可以用vector来保存这些数据.但是当数据量很大的时候使用vector效率就比 ...

  5. 【Linux 进程】exec族函数详解

    exec族的组成: 在Linux中,并不存在一个exec()的函数形式,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **e ...

  6. R中的apply族函数和多线程计算

    一.apply族函数 1.apply  应用于矩阵和数组 # apply # 1代表行,2代表列 # create a matrix of 10 rows x 2 columns m <- ma ...

  7. 从0开始自己用C语言写个shell__01_整体的框架以及fork和exec族函数的理解

    最近才忙完了一个操作系统的作业,让我们用C语言实现一个Shell.总的来说,其实就是让我们 对系统调用有比较深的了解. 首先 介绍一下我的Shell 所实现的功能.1.运行可执行程序 即输入某个 标志 ...

  8. Linux-exec族函数

    1.为什么需要exec族函数 (1).fork子进程是为了执行新程序(fork创建子进程后,子进程和父进程同时被OS调度执行,因此子程序可以单独的执行一个程序,这样程序宏观上将会和父进程程序同时进行) ...

  9. STL之vector常用函数笔记

    STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...

随机推荐

  1. lbs(查看附近的人),看看社交软件如何实现查看附近的人

    最近在做一款移动端棋牌游戏,为了进一步提高用户体验.拉近玩家的距离,我们决定在游戏中加入好友功能,而对于体验好友功能的玩家来说,要是玩牌的时候可以看看附近都有谁在玩牌,跟他们交流交流玩牌心得什么的无疑 ...

  2. AngularJs $http 请求服务

    $http $http是Angular的一个核心服务,它有利于浏览器通过XMLHttpRequest 对象或者 JSONP和远程HTTP服务器交互. $HTTP API 是基于 $q服务暴露的defe ...

  3. 分布式服务框架Zookeeper

    协议介绍 zookeeper协议分为两种模式 崩溃恢复模式和消息广播模式 崩溃恢复协议是在集群中所选举的leader 宕机或者关闭 等现象出现 follower重新进行选举出新的leader 同时集群 ...

  4. Code笔记之:对使用zend加密后的php文件进行解密

    对使用zend加密后的php文件进行解密 使用zend加密后的php文件用notpad++打开会出现类似的乱码 下面使用解密工具进行解密 http://pan.baidu.com/s/1i3n4ysX ...

  5. Exception:A generic error occurred in GDI+

    分析: 一般出现这种问题都是GDI和原数据(比如Bitmap)是同一个实体,只不过是两个引用.换句话说就是这个路径的图片被GDI占用啦. 还有一种情况是路径有问题. 场景一: WPF的Image控件的 ...

  6. XML学习笔记

    XML学习笔记 第一部分:XML简介 我们经常可以听到XML.HTML.XHTML这些语言,后两者比较清楚,一直不是很明白XML是什么,这里做一个总结. XML(eXtensible Markup L ...

  7. matlab 中txt文件(含字符及数值)处理

    matlab 中txt文件(含字符及数值)处理 (2008-08-02 09:45:12) 转载▼ 标签: 杂谈 分类: matlab及C学习 Matlab文件操作及读txt文件ZZ 2008-07- ...

  8. 和安全有关的那些事(非对称加密、数字摘要、数字签名、数字证书、SSL、HTTPS及其他)

    转自http://blog.csdn.net/bluishglc/article/details/7585965 对于一般的开发人员来说,很少需要对安全领域内的基础技术进行深入的研究,但是鉴于日常系统 ...

  9. python中的not具体使用及意思

    python中的not具体使用及意思 name='' while not name: name=raw_input(u'请输入姓名:') print name python中的not具体表示是什么: ...

  10. cookie和浏览器

    XHR API允许应用添加自定义的HTTP首部(通过setRequestHeader()方法),同时也有一些首部都是应用代码不能设定的. Accept-Charset.Accept-Encoding. ...