[R] where does the null come from?

0 views
Skip to first unread message

Wacek Kusnierczyk

unread,
May 13, 2009, 5:23:58 AM5/13/09
to R help
m = matrix(1:4, 2)

apply(m, 1, cat, '\n')
# 1 2
# 3 4
# NULL

why the null?

vQ

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

K. Elo

unread,
May 13, 2009, 5:36:17 AM5/13/09
to r-h...@r-project.org
Hi!

Wacek Kusnierczyk wrote:
> m = matrix(1:4, 2)
>
> apply(m, 1, cat, '\n')
> # 1 2
> # 3 4
> # NULL
>
> why the null?

Could it be the return value of 'cat'. See ?cat, where:

---snip ---
Value
None (invisible NULL).
---snip ---

Kind regrads,
Kimmo

Peter Dalgaard

unread,
May 13, 2009, 5:46:01 AM5/13/09
to Wacek Kusnierczyk, R help
Wacek Kusnierczyk wrote:
> m = matrix(1:4, 2)
>
> apply(m, 1, cat, '\n')
> # 1 2
> # 3 4
> # NULL
>
> why the null?

It comes from unlist()ing a list of NULLs, which in turn are the return
values of cat().

It is arguably a design-buglet not to return list(NULL, NULL), but the
internal logic is to unlist() unless the first element is.recursive (and
NULL is not) or the return values have different length() (and all are
zero). It _is_, however, in accordance with the documentation (see the
Value: section):

--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dal...@biostat.ku.dk) FAX: (+45) 35327907

Wacek Kusnierczyk

unread,
May 13, 2009, 5:55:00 AM5/13/09
to Peter Dalgaard, R help
Peter Dalgaard wrote:
> Wacek Kusnierczyk wrote:
>
>> m = matrix(1:4, 2)
>>
>> apply(m, 1, cat, '\n')
>> # 1 2
>> # 3 4
>> # NULL
>>
>> why the null?
>>
>
> It comes from unlist()ing a list of NULLs, which in turn are the return
> values of cat().
>

yes; i'd think i'd get a list of nulls, but...


> It is arguably a design-buglet not to return list(NULL, NULL), but the
> internal logic is to unlist() unless the first element is.recursive (and
> NULL is not) or the return values have different length() (and all are
> zero). It _is_, however, in accordance with the documentation (see the
> Value: section):
>

... i agree the actual outcome is appropriately explained in the docs.
i don't think it has no merit, but it's a bit surprising at first.

thanks,
vQ

Gabor Grothendieck

unread,
May 13, 2009, 10:43:17 AM5/13/09
to Wacek Kusnierczyk, R help
> out <- apply(m, 1, cat, '\n')
1 3
2 4
> out
NULL

Ted Harding

unread,
May 13, 2009, 10:56:08 AM5/13/09
to R help, Wacek Kusnierczyk
On 13-May-09 14:43:17, Gabor Grothendieck wrote:
>> out <- apply(m, 1, cat, '\n')
> 1 3
> 2 4
>> out
> NULL

Or, more explicitly, from ?cat :

Value:
None (invisible 'NULL').

Ted.

> On Wed, May 13, 2009 at 5:23 AM, Wacek Kusnierczyk
> <Waclaw.Marci...@idi.ntnu.no> wrote:

>> _ _m = matrix(1:4, 2)
>>
>> _ _apply(m, 1, cat, '\n')
>> _ _# 1 2
>> _ _# 3 4
>> _ _# NULL


>>
>> why the null?
>>
>> vQ
>>
>> ______________________________________________
>> R-h...@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-h...@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.H...@manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 13-May-09 Time: 15:56:04
------------------------------ XFMail ------------------------------

Reply all
Reply to author
Forward
0 new messages