Skip to content

New R version - Issue with as.vector coercion on data.frame

5 messages · Marc Weibel, Hugh Parsonage, Bill Dunlap +1 more

#
Hi 

I recently upgraded to the latest r version and got an issue with the command as.vector() on a data.frame object. With the previous R version the returned object was a vector but now it is formatted as a list (see below)
SMI Future SMIM Future Euro Stoxx 50 Future S&P 500 Mini Future FTSE 100 Future Nikkei 225 Future
1      11999        2871                 3692                4150            7419             26853
[1] "data.frame"
$`SMI Future`
[1] 11999

$`SMIM Future`
[1] 2871

$`Euro Stoxx 50 Future`
[1] 3692
[1] "list"


Can you help me on this ? (I solved the issue temporarily with as.vector(t(futures_quotes[1,1:3])) 

Kind regards
Marc Weibel



 <http://www.enisopartners.com/>	
Dr. Marc Weibel
CIO & Partner
Tel: +41 44 286 17 02
Mob: +41 78 892 96 55
E-Mail: marc.weibel at enisopartners.com <mailto: marc.weibel at enisopartners.com>
 
Eniso Partners AG
Claridenstrasse 34 | Postfach | 8022 Z?rich
 
 <https://www.linkedin.com/in/marc-weibel-29a396/>  
Based on previous e-mail correspondence with you and/or an agreement reached with you, Eniso Partners considers itself authorized to contact you via unsecured e-mail. Warning: E-mails can involve SUBSTANTIAL RISKS, e.g. lack of confidentiality, potential manipulation of contents and/or sender's address, incorrect recipient (misdirection), viruses etc. Eniso Partners assumes no responsibility for any loss or damage resulting from the use of e-mails. Eniso Partners recommends in particular that you do NOT SEND ANY SENSITIVE INFORMATION, that you do not include details of the previous message in any reply, and that you enter e-mail address(es) manually every time you write an e-mail. Please notify Eniso Partners immediately if you received this e-mail by mistake or if you do not wish to receive any further e-mail correspondence. If you have received this e-mail by mistake, please completely delete it (and any attachments) and do not forward it or inform any other person of its contents.
#
This is intended. From NEWS, a simple text search for "as.vector" reveals

        
On Mon, 2 May 2022 at 19:19, Marc Weibel <marc.weibel at eniso-partners.com> wrote:
#
In R-4.1.2 (and before) as.vector(aDataFrame) returned aDataFrame,
unchanged.  E.g.,
  4.1.2> aDataFrame <- data.frame(X=101:103, Y=201:203, Z=301:303)
  4.1.2> attr(aDataFrame, "anAttr") <- "an attribute"
  4.1.2> identical(as.vector(aDataFrame), aDataFrame)
  [1] TRUE
  4.1.2> dput(aDataFrame)
  structure(list(X = 101:103, Y = 201:203, Z = 301:303), class =
"data.frame", row.names = c(NA, -3L), anAttr = "an attribute")

In R-4.2.0 it removes the class, names, and row.names attributes, but
leaves other stray attributes alone.  E.g.,
  4.2.0> aDataFrame <- data.frame(X=101:103, Y=201:203, Z=301:303)
  4.2.0> attr(aDataFrame, "anAttr") <- "an attribute"
  4.2.0> identical(as.vector(aDataFrame), aDataFrame)
  [1] FALSE
  4.2.0> dput(aDataFrame)
  structure(list(X = 101:103, Y = 201:203, Z = 301:303), class =
"data.frame", row.names = c(NA, -3L), anAttr = "an attribute")
  4.2.0> dput(as.vector(aDataFrame))
  structure(list(X = 101:103, Y = 201:203, Z = 301:303), anAttr = "an
attribute")

In both versions as.list(aDataFrame) returns the same thing that R-4.1.2's
as.vector(aDataFrame) did.

I think that as.vector() is a function that few people should use.   It has
almost nothing to do with with the notion of a vector in math or physics.

-Bill

On Mon, May 2, 2022 at 2:19 AM Marc Weibel <marc.weibel at eniso-partners.com>
wrote:

  
  
#
R-4.1.2's as.vector(aDataFrame) did.

I sent this too quickly.  I meant to say that as.list(aDataFrame) did the
same in both versions.  Since as.vector(aDataFrame) in 4.1.2 did nothing,
you can just drop that line from your code.

-Bill
On Mon, May 2, 2022 at 8:07 AM Bill Dunlap <williamwdunlap at gmail.com> wrote:

            

  
  
2 days later
#
> In R-4.1.2 (and before) as.vector(aDataFrame) returned aDataFrame,
    > unchanged.  E.g.,
    4.1.2> aDataFrame <- data.frame(X=101:103, Y=201:203, Z=301:303)
    4.1.2> attr(aDataFrame, "anAttr") <- "an attribute"
    4.1.2> identical(as.vector(aDataFrame), aDataFrame)
    > [1] TRUE
    4.1.2> dput(aDataFrame)
    > structure(list(X = 101:103, Y = 201:203, Z = 301:303), class =
    > "data.frame", row.names = c(NA, -3L), anAttr = "an attribute")

    > In R-4.2.0 it

is based on the already existing  as.list.data.frame() which

    > removes the class, names, and row.names attributes, but
    > leaves other stray attributes alone.  E.g.,
    4.2.0> aDataFrame <- data.frame(X=101:103, Y=201:203, Z=301:303)
    4.2.0> attr(aDataFrame, "anAttr") <- "an attribute"
    4.2.0> identical(as.vector(aDataFrame), aDataFrame)
    > [1] FALSE
    4.2.0> dput(aDataFrame)
    > structure(list(X = 101:103, Y = 201:203, Z = 301:303), class =
    > "data.frame", row.names = c(NA, -3L), anAttr = "an attribute")
    4.2.0> dput(as.vector(aDataFrame))
    > structure(list(X = 101:103, Y = 201:203, Z = 301:303), anAttr = "an
    > attribute")

Yes, this was a somewhat "hard" change in an effort to slightly
improve the still unsatisfactory behavior of  as.vector() and is.vector().
and it solved a very long standing FIXME / TODO which wanted
that

   as.vector(df, mode = "list")    should be the same as
   as.list  (df)


   [....]

    > I think that as.vector() is a function that few people should use.   It has
    > almost nothing to do with with the notion of a vector in math or physics.

I tend to agree on that.

The confusion comes from the fact that in R (and S before it, no?)
a list() or an expression() are also a 'vector'  {a generalized one},

but that math/physics/... term of a "vector" is what is an
"atomic vector" in R.

Note that we also ventured into a relatively time confusing effort
to do more about the  as.vector()  <-->  is.vector()  decrepancies.

One step towards that I'd like to take up again --- and was in
the mean time helped by some package maintainers who adapted
their code --- is to make  is.atomic(.)  truly a test for the
above "atomic vector"s.
It currently is *not*, as   is.atomic(NULL)   is TRUE,
but NULL is really not an atomic vector.

So our plan has been that is.atomic(NULL) should become FALSE, so
programmeRs could use is.atomic() checks before using
as.vector(),  which would bring them back to the traditional
realm of 1st semester math where your vector spaces are
typically R^n (or C^n ..)

Martin


    > -Bill

    > On Mon, May 2, 2022 at 2:19 AM Marc Weibel <marc.weibel at eniso-partners.com>
> wrote:
>> Hi
    >> 
    >> I recently upgraded to the latest r version and got an issue with the
    >> command as.vector() on a data.frame object. With the previous R version the
    >> returned object was a vector but now it is formatted as a list (see below)
    >> 
    >> 
    >> > futures_quotes
    >> SMI Future SMIM Future Euro Stoxx 50 Future S&P 500 Mini Future FTSE 100
    >> Future Nikkei 225 Future
    >> 1      11999        2871                 3692                4150
    >> 7419             26853
    >> > class(futures_quotes)
    >> [1] "data.frame"
    >> 
    >> > as.vector(futures_quotes[1,1:3])
    >> $`SMI Future`
    >> [1] 11999
    >> 
    >> $`SMIM Future`
    >> [1] 2871
    >> 
    >> $`Euro Stoxx 50 Future`
    >> [1] 3692
    >> 
    >> > class(as.vector(futures_quotes[1,1:3]))
    >> [1] "list"
    >> 
    >> 
    >> Can you help me on this ? (I solved the issue temporarily with
    >> as.vector(t(futures_quotes[1,1:3]))
    >> 
    >> Kind regards
    >> Marc Weibel
    >> 
    >> 
    >> <http://www.enisopartners.com/>
    >> Dr. Marc Weibel
    >> CIO & Partner
    >> Tel: +41 44 286 17 02
    >> Mob: +41 78 892 96 55
    >> E-Mail: marc.weibel at enisopartners.com <mailto:
    >> marc.weibel at enisopartners.com>
    >> 
    >> Eniso Partners AG
    >> Claridenstrasse 34 | Postfach | 8022 Z?rich
    >> 
    >> <https://www.linkedin.com/in/marc-weibel-29a396/>