Skip to content

How can I improve an ugly, dumb hack

7 messages · Eik Vettorazzi, David Winsemius, Rui Barradas +1 more

#
Hi Folks:

Here's the situation:
x   y
[1,] "a" "d"
[2,] "b" "e"
[3,] "c" "f"

## m is a 2 column character matrix
a b c.x c.y
1 1 4   a   d
2 2 5   b   e
3 3 6   c   f

## But please note (as was remarked in a thread here a couple of months ago)
[1] 3

## d is a ** 3 ** column data frame

Now what I wish to do is programmatically convert d to a 4 column
frame with names c("a","b","x","y"). Of course:

1. The column classes/modes must be preserved (character going to
factor and numeric remaining numeric).

2. I assume that I do not know a priori which of d's
components/columns are matrices and which are vectors.

3. There may be many more columns which are vectors or matrix than
just the three in this little example.

I can easily and sensibly accomplish these 3 tasks, but the problem is
that I run afoul of data frame column naming procedures in doing so,
about which the data.frame Help page says rather enigmatically:

"How the names of the data frame are created is complex, and the rest
of this paragraph is only the basic story." Indeed!
(This, of course, is shorthand for "Go look at the source if you want
to know!" )

Anyway, AFAICT from the Help, any "simple" approach to conversion
using data.frame results in "c.x" and "c.y" for the names of the last
two columns. I **can** get what I want by explicitly constructing the
vector of names via the following ugly hack; my question is, can it be
improved?
a b c.x c.y
1 1 4   a   d
2 2 5   b   e
3 3 6   c   f
[1] 4
$a
NULL

$b
NULL

$c
[1] "x" "y"
##Yuck!
a b x y
1 1 4 a d
2 2 5 b e
3 3 6 c f

Cheers to all,
Bert
#
Hi Bert,
maybe I'm missing the point, but

dd<-cbind(d,m)

does 1, 2 and 3 as desired:

n <- data.frame(nx=letters[7:9], ny = 7:9)
str(cbind(d,m,n))

t<-letters[7:9]
str(cbind(d,t))

cheers.


Am 06.09.2012 18:03, schrieb Bert Gunter:

  
    
#
On Sep 6, 2012, at 9:03 AM, Bert Gunter wrote:

            
I guess this means you are not the one performing the d$c <- m step? If you were under control of that step, you can get different (and more to your liking)  behavior with 'cbind.data.frame':
a b x y
1 1 4 a d
2 2 5 b e
3 3 6 c f
[1] 4
David Winsemius, MD
Alameda, CA, USA
#
On Thu, Sep 6, 2012 at 10:20 AM, David Winsemius <dwinsemius at comcast.net> wrote:
<snipped>
Correct. d is given to me already, as described. I constructed it in
my post only to provide an example of what it might look like. I
apologize for evidently being unclear about this (and I tried real
hard ... sigh....).

-- Bert

  
    
#
Hello,

Try the following.

class(d[3][[1]])
dim(d[3][[1]])

(d2 <- cbind(d[1:2], d[3][[1]]))
dim(d2)

Funny how the original names of 'm' are retrieved.

Rui Barradas

Em 06-09-2012 18:28, Bert Gunter escreveu:
#
On Sep 6, 2012, at 10:28 AM, Bert Gunter wrote:

            
OK, then:
a b x y
1 1 4 a d
2 2 5 b e
3 3 6 c f

HTH;

--
DW
David Winsemius, MD
Alameda, CA, USA
#
As Linus would say:

AGHHH...!

1. Rui's solution clearly violates my conditions 1) and 2) and so does not work.

2. David's violated my UNSTATED condition 4: The order of the columns
cannot be changed. Any matrix "columns" must be expanded "in place"
between their flanking columns of the data frame. Note that the
solution below changes the column order.

... and I apologize for my continuing -- and embarrassing -- lack of clarity.

However, even so, I take some comfort that David needed to resort to
sapply(...) -- so my original "solution" may not be as dumb as I
thought.

In any case, if anyone wants to bother with this further, please
respond offline, as this has continued here long enough.

Thanks.

-- Bert
On Thu, Sep 6, 2012 at 11:06 AM, David Winsemius <dwinsemius at comcast.net> wrote: