[Rd] Assigning a sub-matrix from a data frame to a regular matrixcauses strange behaviour (version 2.6.1) (PR#10799)
Greg Snow
Greg.Snow at imail.org
Thu Feb 21 22:31:24 CET 2008
Part of the problem is that you are not adjusting for the fact that you
are smarter than the computer.
Realize that the way you are doing the assignment requires a lot of
different things behind the scenes and remember that data frames are
basically lists with some extra attributes and matricies are vectors
with some extra attributes.
So in your case the left hand side of the assignment is a vector with 4
slots to fill and the right side is a list with 2 element that need to
be put into the 4 slots.
You realize that each of the 2 elements of the list have 2 elements for
a total of 4 and being the smart human that you are (assuming your not a
turing machine) you think, that is a total of 4 elements, one for each
slot. But the computer is not smart and unless someone tells it to look
for this type of case and convert, it is not smart enough to do it on
its own and instead puts a list element into each slot of the vector
(changing it to a list in the process).
Since the programmers of these routines did not anticipate you asking
the computer to do such things, you can tell the computer how you really
want the assignment to take place by turning the 4 numbers into
something the computer knows about. Wrap the d[,1:2] in either
as.matrix or unlist and it does what I expect you expect it to do.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111
> -----Original Message-----
> From: r-devel-bounces at r-project.org
> [mailto:r-devel-bounces at r-project.org] On Behalf Of david at opsi.co.za
> Sent: Tuesday, February 19, 2008 5:50 AM
> To: r-devel at stat.math.ethz.ch
> Cc: R-bugs at biostat.ku.dk
> Subject: [Rd] Assigning a sub-matrix from a data frame to a
> regular matrixcauses strange behaviour (version 2.6.1) (PR#10799)
>
> See below:
>
>
>
> > d = data.frame(v1=1:2,v2=1:2)
>
> > x = matrix(0,2,2)
>
> > x[,1:2] = d[,1:2]
>
> > x
>
> [[1]]
>
> [1] 1 2
>
>
>
> [[2]]
>
> [1] 1 2
>
>
>
> [[3]]
>
> [1] 1 2
>
>
>
> [[4]]
>
> [1] 1 2
>
>
>
> >
>
>
>
> Thanks for all the work in R. Still love it.
>
>
>
> David
>
>
>
>
>
> David Lubinsky
>
> Director
>
> OPSI Systems
>
>
>
> Phone: +27 11 880 7951
>
> Cell: +27 82 452 9556
>
> URL: <http://www.opsi.co.za> www.opsi.co.za
>
> Email: <mailto:david at opsi.co.za> david at opsi.co.za
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
More information about the R-devel
mailing list