A major question is why you ask how to use the subset function rather than 
asking how to get your job done.

As you note, the simple way to get the first N items is to use indexing. If you 
absolutely positively insist on using subset, place your data into something 
like a data.frame and add a column and use that.

Say you have N items, so that length() or nrow() return N. You can add a column 
called index whose entries are 1:N and do your subset to get all rows where the 
condition is "index <= N" or something like that. 

If you want just the first N (not N at a time in several tries) simply used 
head(data, N) and that gets you the first N.

And if you are interested in learning other packages like dplyr in the 
tidyverse, it has oodles of ways to select based on row numbers. One example is 
top_n(...) and another is slice(first, last)  and in some contexts such as the 
filter() function, there is a sort of internal function you can use called n() 
that contains the index of the entry within any grouping so filter(mydata, n() 
<= 20) might get what you want.

Note that the above requires care as some things work on vectors and others 
assume a data.frame, albeit you can make a data.frame containing a single 
vector.


-----Original Message-----
From: R-help <r-help-boun...@r-project.org> On Behalf Of Upananda Pani
Sent: Sunday, February 5, 2023 2:33 PM
To: Andrés González Carmona <andre...@ugr.es>
Cc: r-help <r-help@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, <andre...@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-h...@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@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.

______________________________________________
R-help@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.

Reply via email to