Re: [R] Dynamic string as element name in a list

From: Paul Lemmens <paul.lemmens_at_gmail.com>
Date: Thu, 27 Mar 2008 15:45:47 +0100

Hi,

Sorry yes, I forgot to give comment about the lapply().

> > foo <- function(x) {
> > out <- list()
> > lapply(x$clipno, function(c) {
> > clipname <- x$clipname
> > # stuff
> >
> > out[[clipname]] <- rnorm(5)
> > })
> > return(out)
> > }
> >

So x is a dataframe with the data from one subject that has viewed multiple clips in two variations. x contains both the responses as well as info about the clips in factors and responses in integers. So for each clip (using function(c) call in lapply() to be able to access/subset data from x), I extract the name and other info (using c), store that info in a list element named after the clip. Finally return the full list of clips with associated data.

Mvg,
Paul

On Thu, Mar 27, 2008 at 3:23 PM, Henrique Dallazuanna <wwwhsd_at_gmail.com> wrote:
> HI,
>
> I don't understand why you're using lapply.
>
> Please provide a example of your 'x' data.frame
>
> str(x)
>
>
> On 27/03/2008, Paul Lemmens <paul.lemmens_at_gmail.com> wrote:
>
>
> > Hi Henrique,
> >
> >
> > On Thu, Mar 27, 2008 at 2:52 PM, Henrique Dallazuanna <wwwhsd_at_gmail.com> wrote:
> > > Try this:
> > >
> > > foo <- function(x)
> > > {
> > > clipname <- "LK"
> > > out <- list()
> > > out[[clipname]] <- rnorm(5)
> > > return(out)
> > > }
> >
> > That easy ... Hadn't thought of it. But now I have a refinement in foo()
> >
> > foo <- function(x) {
> > out <- list()
> > lapply(x$clipno, function(c) {
> > clipname <- x$clipname
> > # stuff
> >
> > out[[clipname]] <- rnorm(5)
> > })
> > return(out)
> > }
> >
> >
> > But this returns the/an empty list?
> >
> >
> > Best regards,
> >
> > Paul Lemmens
> >
>
>
> > ______________________________________________
> > R-help_at_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.
> >
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>



R-help_at_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. Received on Thu 27 Mar 2008 - 18:55:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 27 Mar 2008 - 21:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive