[R] loop with splitted data

Ling, Gary (Electronic Trading) Gary_Ling at ml.com
Wed Sep 10 18:08:11 CEST 2008


YES! many many thanks, Jim.
I know it'd be simple, yet I can't believe it's that simple. =)

-gary

-----Original Message-----
From: jim holtman [mailto:jholtman at gmail.com] 
Sent: Wednesday, September 10, 2008 11:55 AM
To: Ling, Gary (Electronic Trading)
Cc: Peter Dalgaard; r-help at r-project.org
Subject: Re: [R] loop with splitted data


Is this what you want:

> lapply(z, '[[', 1)
[[1]]
[1] 1 2 3

[[2]]
[1] 11 12 13

> lapply(z, '[[', 2)
[[1]]
[1] 4 5 6

[[2]]
[1] 14 15 16

>


On Wed, Sep 10, 2008 at 11:43 AM, Ling, Gary (Electronic Trading)
<Gary_Ling at ml.com> wrote:
> Hi R users, I have a question which is some what related to this thread.
>
> Here is the setup:
> z is a list of lists, with no names.
> zz is same is z, but with names.
> with names on zz, I can crawl on the data with lapply, but I don't know how do the same on z (without names). Can someone help?
>
>
> ### === here is the code, which explains more clear of my problem
> x <- list(1:3,4:6)
> y <- list(11:13,14:16)
> z <- list(x,y)
>
> xx <- x
> names(xx) <- c("1","2")
> yy <- y
> names(yy) <- c("1","2")
> zz <- list(xx,yy)
>
> lapply(zz, "[[", "1")
> lapply(zz, "[[", "2")
> # Question: how can I get the same thing from z?
> # i.e. lapply(z, "[[", <???>), what is <???> so that
> # I can get the same values as lapply(zz, "[[", "1").
> # I tried <???>=[[1]], <???>=[1], <???>="[[". They all don't work.
> # I feel that I need a string or something to tell lapply what I want.
>
> This is a simplify version of my problem; in my case, I can't name all the sub-lists, please help.
>
> Thanks in advance,
> Gary
>
>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Dalgaard
> Sent: Tuesday, August 26, 2008 6:27 PM
> To: jim holtman
> Cc: r-help at r-project.org
> Subject: Re: [R] loop with splitted data
>
>
> jim holtman wrote:
>> Is this what you want:
>>
>>
>>> y = c(rep(2,5),rep(3,5))
>>> ma <- data.frame(x = 1:10, y=y  )
>>> splitted <- split(ma, ma$y)
>>> for (counter in (min(ma$y):max(ma$y)))
>>>
>> + {
>> + cat(counter, ":", splitted[[as.character(counter)]]$x, '\n')
>> + }
>> 2 : 1 2 3 4 5
>> 3 : 6 7 8 9 10
>>
>>
> But maybe this is what he really wanted:
>
>  > lapply(splitted,"[[", "x")
> $`2`
> [1] 1 2 3 4 5
>
> $`3`
> [1]  6  7  8  9 10
>
> or even
>
>  > split(ma$x, ma$y)
> $`2`
> [1] 1 2 3 4 5
>
> $`3`
> [1]  6  7  8  9 10
>
> --
>   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.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907
>
> ______________________________________________
> 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.
> --------------------------------------------------------
>
> This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list