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
On 05/02/2023 2:59 p.m., Rolf Turner wrote:> 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.Just to build the mountain a little higher, I would use subset(p, seq_along(p) <= 20) You should generally avoid expressions like "1:length(p)", because they don't do what you would expect in the unusual case that p is length 0. Even in cases where you know p is not length 0, it's a good habit to get into so that in the future you don't get surprised. Duncan Murdoch
Hi Rolf, Thank you so much. I was just curious to know that. I am glad that i got the input from all of you. I am grateful to you for clarifying. With sincere gratitude, Upananda On Mon, Feb 6, 2023 at 1:29 AM Rolf Turner <r.turner at auckland.ac.nz> wrote:> 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 > >[[alternative HTML version deleted]]