[R] why as.vector can't make x to be a vector?
David Carlson
dcarlson at tamu.edu
Sun Dec 15 20:35:28 CET 2013
According to the documentation (?as.vector), "All attributes are removed from the result if it is of an atomic mode, but not in general for a list result."
data.frames are lists so
> is.vector(as.vector(x))
[1] FALSE
However if you convert using unlist() or as.matrix(), you will get a vector:
> is.vector(unlist(x))
[1] TRUE
> is.vector(as.vector(as.matrix(x)))
[1] TRUE
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Duncan Murdoch
Sent: Sunday, December 15, 2013 6:12 AM
To: 水静流深; r-help
Subject: Re: [R] why as.vector can't make x to be a vector?
On 13-12-15 6:48 AM, 水静流深 wrote:
>> x=read.table(text="
> + Qtr1 Qtr2 Qtr3 Qtr4
> + 2010 1.8 8.0 6.0 3.0
> + 2011 2.0 11.0 7.0 3.5
> + 2012 2.5 14.0 8.0 4.2
> + 2013 3.0 15.2 9.5 5.0",sep="",header=TRUE)
>> x
> Qtr1 Qtr2 Qtr3 Qtr4
> 2010 1.8 8.0 6.0 3.0
> 2011 2.0 11.0 7.0 3.5
> 2012 2.5 14.0 8.0 4.2
> 2013 3.0 15.2 9.5 5.0
>> as.vector(x)
> Qtr1 Qtr2 Qtr3 Qtr4
> 2010 1.8 8.0 6.0 3.0
> 2011 2.0 11.0 7.0 3.5
> 2012 2.5 14.0 8.0 4.2
> 2013 3.0 15.2 9.5 5.0
>> class(as.vector(x))
> [1] "data.frame"
Data frames are lists, which are already vectors.
Duncan Murdoch
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list