[R] How to call subset in a for loop?

David Reiner David.Reiner at xrtrading.com
Wed Jan 26 15:35:10 CET 2011


you were caught by the '=' versus '==' error ;-)
and Henrique's elegant one-liner avoids the problem altogether.

-- David

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Henrique Dallazuanna
Sent: Wednesday, January 26, 2011 6:00 AM
To: Aditya Bhagwat
Cc: r-help at r-project.org
Subject: Re: [R] How to call subset in a for loop?

Try this:

split(myDF, myDF$myField)

On Wed, Jan 26, 2011 at 8:18 AM, Aditya Bhagwat <bhagwataditya at gmail.com>wrote:

> Dear all,
>
> I have a data frame 'myDf', in which one of the fields 'myField' can have
> several possible values. To extract the observations for which it has value
> "A", I can do:
>
> subset(myDf, myField="A")
>
> However, when I try to do this within a loop, it doesn't work, it returns
> everything, and not a subset
>
> for (currField in c("A", "B", "C")){
>   subset(myDf, myField=currField)
> }
>
> How should I modify the call of subset in the loop to make it work?
>
> Thanks for your help!
>
> Adi
>
> --
> Aditya Bhagwat
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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

        [[alternative HTML version deleted]]



This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws.  Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates.

THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND.  TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.



More information about the R-help mailing list