Skip to content
Back to formatted view

Raw Message

Message-ID: <efb536d50905071838m16eb4b00u68b03e26be2b6f4e@mail.gmail.com>
Date: 2009-05-08T01:38:53Z
From: Sarah Goslee
Subject: paste with apply, spaces and NA
In-Reply-To: <77EB52C6DD32BA4D87471DCD70C8D70001296ECD@NA-PA-VBE03.na.tibco.com>

Hi,

On Thu, May 7, 2009 at 6:36 PM, William Dunlap <wdunlap at tibco.com> wrote:

> Do you get similar results rif you use 10 instead of NA
> in your examples, with more spaces if you use 10000?

No. Only that particular combination of NA values and strings with
spaces gives the odd result.

> I think this has to do with apply's call to as.matrix(X)
> when X is a data.frame with mixed numeric and character
> or factor columns. ?It calls format() on each numeric column
> to convert its elements to strings with the same number
> of characters in each string. ?apply() rarely gives you what
> you want on such mixed data.frames.

The most perplexing bit, though, is that apply() adds the extra
spaces for _all_ columns, not just the ones with NA values.
I assume that's to do with the internals of apply; it just wasn't
something I expected.

> Pasting the columns without apply is faster and will
> give the correct results. ?I find it convenient to use
> do.call here:
> ? ?> do.call(`paste`, c(unname(test3),list(sep=",")))
> ? ?[1] "1,a,a b,2" ?"1,a,a b,2" ?"1,a,a b,2" ?"1,a,a b,NA" "1,a,a b,2"
> (unname(as.list(test3)) would be a bit more legal. ?The unname
> would be required if one of the column names was 'sep' or
> 'collapse'.)

Yes, there are other possibilities. I'm not as adept with do.call() as
I should be, and missed that one - thanks. And of course, this was
part of a larger more complex problem, and not truly an end of itself.

Thanks for the thoughts,
Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org