Dear All, I want to create a vector p and extract first 20 observations using subset function based on logical condition. My code is below p <- 0:100 I know i can extract the first 20 observations using the following command. q <- p[1:20] But I want to extract the first 20 observations using subset function which requires a logical condition. I am not able to frame the logical condition. The code should be q <- subset(p, logical condition) I am not able to do it. Please let me know what you think. Best regards, Upananda [[alternative HTML version deleted]]
Is this a homework problem? --Chris Ryan -- Sent from my Android device with K-9 Mail. Please excuse my brevity. On February 5, 2023 9:07:03 AM EST, Upananda Pani <upananda.pani at gmail.com> wrote:>Dear All, > >I want to create a vector p and extract first 20 observations using subset >function based on logical condition. > >My code is below > >p <- 0:100 > >I know i can extract the first 20 observations using the following command. > >q <- p[1:20] > >But I want to extract the first 20 observations using subset function which >requires a logical condition. I am not able to frame the logical condition. > >The code should be > >q <- subset(p, logical condition) > >I am not able to do it. Please let me know what you think. > >Best regards, >Upananda > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
On Sun, 5 Feb 2023 19:37:03 +0530 Upananda Pani <upananda.pani at gmail.com> wrote:> Dear All, > > I want to create a vector p and extract first 20 observations using > subset function based on logical condition. > > My code is below > > p <- 0:100 > > I know i can extract the first 20 observations using the following > command. > > q <- p[1:20] > > But I want to extract the first 20 observations using subset function > which requires a logical condition. I am not able to frame the > logical condition. > > The code should be > > q <- subset(p, logical condition) > > I am not able to do it. Please let me know what you think.I think that you are manufacturing an unnecessary difficulty. If you want the first 20 entries of p, use p[1:20]!!! However, if you obdurately *insist* on using subset() you could do subset(p,(1:length(p)) <= 20) This works, but makes a mountain out of a molehill. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Stats. Dep't. phone: +64-9-373-7599 ext. 89622 Home phone: +64-9-480-4619
Hi Upananda, As a couple of respondents noted, you only need the logical statement, not subset(). You did "frame the logical condition" in your example, for if you use the extraction operator "[" this will work: subset(p,(1:length(p)) <= 20) as Jeff already pointed out. Obviously it is easier to write: p[(1:length(p)) <= 20] Let's say you want to select the odd numbers in p: p<-1:10 as.logical(p%%2) [1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE p[as.logical(p%%2)] [1] 1 3 5 7 9 The as.logical() has to be included as the extractor function tries use integers as indices So the problem is really to reduce your selection criteria to a vector of TRUER/FALSE values. Jim On Mon, Feb 6, 2023 at 1:07 AM Upananda Pani <upananda.pani at gmail.com> wrote:> > Dear All, > > I want to create a vector p and extract first 20 observations using subset > function based on logical condition. > > My code is below > > p <- 0:100 > > I know i can extract the first 20 observations using the following command. > > q <- p[1:20] > > But I want to extract the first 20 observations using subset function which > requires a logical condition. I am not able to frame the logical condition. > > The code should be > > q <- subset(p, logical condition) > > I am not able to do it. Please let me know what you think. > > Best regards, > Upananda > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.