vector族函数
2016-03-20 13:41
239 查看
本文原创,转载请注明出处,本人Q1273314690
vector(mode="logical",length=0)
as.vector(x,mode="any")#下面没写明默认值,所以Usage这里写的就算默认值
is.vector(x,mode="any")
这三个函数都是同族的
vectorproducesavectorofthegivenlengthandmode.
vector()产生一个指定模式和长度的向量
as.vector,ageneric(泛型),attemptstocoerceitsargumentintoavectorofmodemode(thedefaultistocoercetowhichevervectormodeismostconvenient):iftheresultisatomicallattributesareremoved(结果是原生类型,则所有的属性将被移除).
as.vector()转化为一个指定模式的向量
is.vectorreturnsTRUEifxisavectorofthespecifiedmodehavingnoattributesotherthannames.ItreturnsFALSEotherwise.
如果x是一种只含有names属性的特定模式类型的向量,则返回T(所以判断的时候要加上类型啊~(mode="any"))
判断是否为指定模式的向量(默认的模式是“any”)
Arguments
mode
characterstringnaminganatomicmode(应指数值,字符,逻辑等类型下面detail有解释)or"list"or"expression"or(exceptforvector)"any".
length
anon-negativeintegerspecifyingthedesiredlength.Foralongvector,i.e.,length>.Machine$integer.max,ithastobeoftype"double".Supplyinganargumentoflengthotherthanoneisanerror.
x
anRobject.
Details
Theatomicmodesare"logical","integer","numeric"(synonym(同义词)"double"),"complex","character"and"raw".
Ifmode="any",is.vectormayreturnTRUEfortheatomicmodes,listandexpression.Foranymode,itwillreturnFALSEifxhasanyattributesexceptnames.(ThisisincompatiblewithS.)Ontheotherhand,as.vectorremovesallattributesincludingnamesforresultsofatomicmode(butnotthoseofmode"list"nor"expression").
如果mode是any,则is.vector将会对原生模式,列表,表达式都返回T,只要含有除了names属性外的其他属性就返回F。
as.vector()将去除原生类型中的所有属性(包括names),但list和expression不会
Notethatfactorsarenotvectors;is.vectorreturnsFALSEandas.vectorconvertsafactortoacharactervectorformode="any".
因子并不是向量,as.vector将其转化为字符向量
Value
Forvector,avectorofthegivenlengthandmode.LogicalvectorelementsareinitializedtoFALSE,numericvectorelementsto0,charactervectorelementsto"",rawvectorelementstonulbytesandlist/expressionelementstoNULL.
对于给定长度和模式的向量。
逻辑向量用F初始化其元素,数值向量用0初始化其元素。
字符向量用""初始化其元素,list/expression用NULL初始化其元素
Foris.vector,TRUEorFALSE.is.vector(x,mode="numeric")canbetrueforvectorsoftypes"integer"or"double"whereasis.vector(x,mode="double")canonlybetrueforthoseoftype"double".
使用is.vector的时候,最好指定mode(默认是nay),以便于更准确的判断
Methodsforas.vector()
Writersofmethodsforas.vectorneedtotakecaretofollowtheconventionsofthedefaultmethod.Inparticular
Argumentmodecanbe"any",anyoftheatomicmodes,"list","expression","symbol","pairlist"oroneofthealiases(别名)"double"and"name".
Thereturnvalueshouldbeoftheappropriatemode.Formode="any"thismeansanatomicvectororlist.
Attributesshouldbetreatedappropriately:inparticularwhentheresultisanatomicvectorthereshouldbenoattributes,notevennames.
is.vector(as.vector(x,m),m)shouldbetrueforanymodem,includingthedefault"any".
Note
as.vectorandis.vectorarequitedistinctfromthemeaningoftheformalclass"vector"inthemethodspackage,andhenceas(x,"vector")andis(x,"vector").
Notethatas.vector(x)isnotnecessarilyanulloperationifis.vector(x)istrue:anynameswillberemovedfromanatomicvector.
如果is.vector(x)的结果是T,as.vector(x)不一定是一个空操作(因为我虽然还是向量,但可以祛除names属性呀,所以我还是操作的),这里的x是同一个x
例子1
因为数据框是列表的一种,列表不能转化为mode="numeric"?
试验
我自己再试了下
得出结论,as.vector真是鸡肋
//2016年3月18日22:32:15
然而,你并不能这么说它鸡肋,他是可以成功将矩阵转为向量的,只是不能对数据框罢了,因为数据本就是存不同类型的数据的,你并不能强求人家转,而且这也没什么意义。
于是引出下一个问题:
如何友好的从数据框中取出向量?
例子2
主要为了说明as.vector()会祛除names属性
例子3
来自为知笔记(Wiz)
vector(mode="logical",length=0)
as.vector(x,mode="any")#下面没写明默认值,所以Usage这里写的就算默认值
is.vector(x,mode="any")
这三个函数都是同族的
vectorproducesavectorofthegivenlengthandmode.
vector()产生一个指定模式和长度的向量
as.vector,ageneric(泛型),attemptstocoerceitsargumentintoavectorofmodemode(thedefaultistocoercetowhichevervectormodeismostconvenient):iftheresultisatomicallattributesareremoved(结果是原生类型,则所有的属性将被移除).
as.vector()转化为一个指定模式的向量
is.vectorreturnsTRUEifxisavectorofthespecifiedmodehavingnoattributesotherthannames.ItreturnsFALSEotherwise.
如果x是一种只含有names属性的特定模式类型的向量,则返回T(所以判断的时候要加上类型啊~(mode="any"))
判断是否为指定模式的向量(默认的模式是“any”)
Arguments
mode
characterstringnaminganatomicmode(应指数值,字符,逻辑等类型下面detail有解释)or"list"or"expression"or(exceptforvector)"any".
length
anon-negativeintegerspecifyingthedesiredlength.Foralongvector,i.e.,length>.Machine$integer.max,ithastobeoftype"double".Supplyinganargumentoflengthotherthanoneisanerror.
x
anRobject.
Details
Theatomicmodesare"logical","integer","numeric"(synonym(同义词)"double"),"complex","character"and"raw".
Ifmode="any",is.vectormayreturnTRUEfortheatomicmodes,listandexpression.Foranymode,itwillreturnFALSEifxhasanyattributesexceptnames.(ThisisincompatiblewithS.)Ontheotherhand,as.vectorremovesallattributesincludingnamesforresultsofatomicmode(butnotthoseofmode"list"nor"expression").
如果mode是any,则is.vector将会对原生模式,列表,表达式都返回T,只要含有除了names属性外的其他属性就返回F。
as.vector()将去除原生类型中的所有属性(包括names),但list和expression不会
Notethatfactorsarenotvectors;is.vectorreturnsFALSEandas.vectorconvertsafactortoacharactervectorformode="any".
因子并不是向量,as.vector将其转化为字符向量
Value
Forvector,avectorofthegivenlengthandmode.LogicalvectorelementsareinitializedtoFALSE,numericvectorelementsto0,charactervectorelementsto"",rawvectorelementstonulbytesandlist/expressionelementstoNULL.
对于给定长度和模式的向量。
逻辑向量用F初始化其元素,数值向量用0初始化其元素。
字符向量用""初始化其元素,list/expression用NULL初始化其元素
Foris.vector,TRUEorFALSE.is.vector(x,mode="numeric")canbetrueforvectorsoftypes"integer"or"double"whereasis.vector(x,mode="double")canonlybetrueforthoseoftype"double".
使用is.vector的时候,最好指定mode(默认是nay),以便于更准确的判断
Methodsforas.vector()
Writersofmethodsforas.vectorneedtotakecaretofollowtheconventionsofthedefaultmethod.Inparticular
Argumentmodecanbe"any",anyoftheatomicmodes,"list","expression","symbol","pairlist"oroneofthealiases(别名)"double"and"name".
Thereturnvalueshouldbeoftheappropriatemode.Formode="any"thismeansanatomicvectororlist.
Attributesshouldbetreatedappropriately:inparticularwhentheresultisanatomicvectorthereshouldbenoattributes,notevennames.
is.vector(as.vector(x,m),m)shouldbetrueforanymodem,includingthedefault"any".
Note
as.vectorandis.vectorarequitedistinctfromthemeaningoftheformalclass"vector"inthemethodspackage,andhenceas(x,"vector")andis(x,"vector").
Notethatas.vector(x)isnotnecessarilyanulloperationifis.vector(x)istrue:anynameswillberemovedfromanatomicvector.
如果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"))
Errorinas.vector(data.frame(x=1:3,y=5:7),mode="numeric"):
(list)objectcannotbecoercedtotype'double'
因为数据框是列表的一种,列表不能转化为mode="numeric"?
试验
>as.vector(data.frame(x=1:3,y=5:7),mode="list")
xy
115
226
337
>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")
Errorinas.vector(test,mode="numeric"):
(list)objectcannotbecoercedtotype'double'
得出结论,as.vector真是鸡肋
//2016年3月18日22:32:15
然而,你并不能这么说它鸡肋,他是可以成功将矩阵转为向量的,只是不能对数据框罢了,因为数据本就是存不同类型的数据的,你并不能强求人家转,而且这也没什么意义。
于是引出下一个问题:
例子2
>x<-c(a=1,b=2)
>is.vector(x)
[1]TRUE
>as.vector(x)
[1]12
>all.equal(x,as.vector(x))##FALSE
[1]"namesfortargetbutnotforcurrent"
>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
相关文章推荐
- 复利计算--web版--总结--软件工程
- 《Linux内核分析》课程第四周学习总结
- 此博客内博文均转自我个人的博客网站!
- 一次向svn中增加所有新增文件 svn add all new files
- 关于串口复用造成的意外及总结
- ExtJs之VTYPE验证
- 缺少动态连接库.so--cannot open shared object file: No such file or directory
- LeetCodeOJ:1Two Sum
- Linux系统搭建JDK环境
- 硬件改版引起的I2C异常
- poj1050
- fore end common url
- RMI介绍与使用
- UESTC1012(水)
- html5标签
- 使用可靠的I2C代码
- maven -- 配置
- Python进阶01 文件处理
- (2)apply函数及其源码
- IE6下设置div的height:1px失效