s-news
[Top] [All Lists]

Re: [S] Feature or bug?

Subject: Re: [S] Feature or bug?
From: "Paul Y. Peng" <ypeng@math.mun.ca>
Date: Fri, 23 Jul 1999 15:06:26 -0230
Cc: s-news <s-news@wubios.wustl.edu>
Organization: http://www.math.mun.ca/~ypeng
References: <Pine.GSO.4.05.9907230828510.29439-100000@auk.stats>
Sender: owner-s-news@wubios.wustl.edu
I thank Professor Ripley for pinpointing my problem. Apparently
I took advantage of a non-standard way of using lapply. Now I have
to explicitly convert the vector into a list to keep the names,
ie. converting
        c("a"=1, "b"=2)
into
        list(c("a"=1), c("b"=2))

so that the function in lapply can access the elements of
the vector as well as the names of the elements. Unfortunately
as.list() won't do this conversion.

Paul.

Prof Brian D Ripley wrote:
> 
> > In S-PLUS 3.3 (DEC Alpha) and S-PLUS 2000,
> >
> > > z <- c("a"=1, "b"=2)
> > > lapply(z, function(x)names(x))
> > $a:
> > [1] "a"
> >
> > $b:
> > [1] "b"
> >
> > But in S-PLUS 5.1 (Linux),
> >
> > > z <- c("a"=1, "b"=2)
> > > lapply(z, function(x)names(x))
> > $a:
> > NULL
> >
> > $b:
> > NULL
> >
> > Is this a new feature of S-PLUS 5? Do I miss anything in
> > S-PLUS 5? Thank you for your help.
> 
> No, you missed something in S-PLUS 3.3. The argument of lapply is
> supposed to be a list:
> 
> Apply a Function to Components of a List
> 
> DESCRIPTION:
>        Returns a list which is the result of a function  that  is
>        applied to each element of a list.
> 
> USAGE:
>        lapply(X, FUN, ...)
> 
> REQUIRED ARGUMENTS:
> X:     a list.
> 
> > z <- c("a"=1, "b"=2)
> > is.list(z)
> [1] F
> 
> In S-PLUS 5.1
> 
> > as.list(z)
> $a:
> [1] 1
> 
> $b:
> [1] 2
> 
> Those components have no names.
> 
> In S-PLUS 3.4
> 
> > as.list(z)
> [[1]]:
> [1] 1
> 
> [[2]]:
> [1] 2
> > lapply(as.list(z), names)
> [[1]]:
> NULL
> 
> [[2]]:
> NULL
> > lapply(z, names)
> $a:
> [1] "a"
> 
> $b:
> [1] "b"
> 
> which seems rather inconsistent.
> 
> Now if you had complained about
> > sapply(z, names) # 3.4
>    a   b
>  "a" "b"
> > sapply(z, names) # 5.1
> $a:
> NULL
> 
> $b:
> NULL
> 
> I would agree, as
> 
>    X
>           any S-PLUS object; usually a list. Missing values ( NAs) are
>  ...
> 
> However, just names(z) or as.list(names(z)) would do in your problem.
> 
> --
> Brian D. Ripley,                  ripley@stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272860 (secr)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>