[R] Extracting data using subset function

Upananda Pani up@n@nd@@p@n| @end|ng |rom gm@||@com
Mon Feb 6 10:14:06 CET 2023


Hi Avi,

Thanks a lot. This worked well.

My sincere gratitude to you for teaching me. I am also grateful to
previous forum contributors, who have given me the various dimensions to
think and solve the problem. It has really helped to enhance my
understanding of subsetting function.

Thank you once again to all of you.

Regards,
Upananda Pani

On Mon, Feb 6, 2023 at 4:29 AM <avi.e.gross using gmail.com> wrote:

> In reading the post again, it sounds like the question is how to create a
> logical condition that translates as 1:N is TRUE. Someone hinted along
> those lines.
>
> So one WAY I might suggest is you construct a logical vector as shown
> below. I give an example of a bunch of 9 primes and you want only the first
> 5.
>
> > vec <- c(1,2,3,5,7,11,13,17, 19)
> > length(vec)
> [1] 9
> > N <- 5
> > choose <- rep(TRUE, N)
> > choose
> [1] TRUE TRUE TRUE TRUE TRUE
> > subset(vec, choose)        # will fail as the remaining are recycled or
> assumed to be true
> [1]  1  2  3  5  7 11 13 17 19
> > tot = length(vec)
> > choose <- c(rep(TRUE, N), rep(FALSE, tot - N))
> > choose
> [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
> > subset(vec, choose)
> [1] 1 2 3 5 7
>
> The end shows you need to create N Boolean TRUE values and tot-N FALSE to
> make a vector of the same length as the first so everything is indexed.
>
> Does something like this meet your needs?
>
> Realistically, the above technique generalizes to more complex cases
> decently. But sometimes, head(0 and other things I mentioned earlier work
> quite well.
>
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Upananda Pani
> Sent: Sunday, February 5, 2023 2:33 PM
> To: Andrés González Carmona <andresgc using ugr.es>
> Cc: r-help <r-help using r-project.org>
> Subject: Re: [R] Extracting data using subset function
>
> Thank you. It means we can not use the subset function here.
>
> Regards
>
> On Mon, 6 Feb, 2023, 00:53 Andrés González Carmona, <andresgc using ugr.es>
> wrote:
>
> > From ?subset:
> > Warning
> >
> > This is a convenience function intended for use interactively. For
> > programming it is better to use the standard subsetting functions like
> > [ <http://127.0.0.1:21786/library/base/help/%5B>, and in particular
> > the non-standard evaluation of argument subset can have unanticipated
> > consequences.
> >
> > El 05/02/2023 a las 15:07, Upananda Pani escribió:
> >
> > 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 using r-project.org
> > mailing list -- To UNSUBSCRIBE and more,
> > seehttps://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r
> > -help__;!!D9dNQwwGXtA!SLdwTGqSfhwUo4CfbUJFeL7hETw64hOG8MQ0FK_o5YdnvVHa
> > Op9Qxs4D7d5e10hj3YQ8EuaFc8qbnkynoP5dEA$
> > PLEASE do read the posting guide
> > https://urldefense.com/v3/__http://www.R-project.org/posting-guide.htm
> > l__;!!D9dNQwwGXtA!SLdwTGqSfhwUo4CfbUJFeL7hETw64hOG8MQ0FK_o5YdnvVHaOp9Q
> > xs4D7d5e10hj3YQ8EuaFc8qbnkwuss43hA$
> > and provide commented, minimal, self-contained, reproducible code.
> >
> > --
> > * Andrés González Carmona *
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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.
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list