[R] paste with apply, spaces and NA

Sarah Goslee sarah.goslee at gmail.com
Fri May 8 03:38:53 CEST 2009


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



More information about the R-help mailing list