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 at 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, seehttps://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!D9dNQwwGXtA!SLdwTGqSfhwUo4CfbUJFeL7hETw64hOG8MQ0FK_o5YdnvVHaOp9Qxs4D7d5e10hj3YQ8EuaFc8qbnkynoP5dEA$ > PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!D9dNQwwGXtA!SLdwTGqSfhwUo4CfbUJFeL7hETw64hOG8MQ0FK_o5YdnvVHaOp9Qxs4D7d5e10hj3YQ8EuaFc8qbnkwuss43hA$ > and provide commented, minimal, self-contained, reproducible code. > > -- > * Andr?s Gonz?lez Carmona * >[[alternative HTML version deleted]]
No, it means what it says: it is best used interactively rather than in functions. That is not saying you cannot use it... merely that you should probably use it interactively. The fact is, though, that integer indexing is much simpler and clearer for your particular example than subset is. q <- p[1:20] q2 <- subset( p, 1:100 <= 20) # 1:100 are positions On February 5, 2023 11:33:16 AM PST, Upananda Pani <upananda.pani at gmail.com> wrote:>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 at 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, seehttps://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!D9dNQwwGXtA!SLdwTGqSfhwUo4CfbUJFeL7hETw64hOG8MQ0FK_o5YdnvVHaOp9Qxs4D7d5e10hj3YQ8EuaFc8qbnkynoP5dEA$ >> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!D9dNQwwGXtA!SLdwTGqSfhwUo4CfbUJFeL7hETw64hOG8MQ0FK_o5YdnvVHaOp9Qxs4D7d5e10hj3YQ8EuaFc8qbnkwuss43hA$ >> and provide commented, minimal, self-contained, reproducible code. >> >> -- >> * Andr?s Gonz?lez Carmona * >> > > [[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.-- Sent from my phone. Please excuse my brevity.
@vi@e@gross m@iii@g oii gm@ii@com
2023-Feb-05 21:12 UTC
[R] Extracting data using subset function
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-bounces at r-project.org> On Behalf Of Upananda Pani Sent: Sunday, February 5, 2023 2:33 PM To: Andr?s Gonz?lez Carmona <andresgc at ugr.es> Cc: r-help <r-help at 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 at 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 at 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 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.
?s 19:33 de 05/02/2023, Upananda Pani escreveu:> 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 at 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, seehttps://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!D9dNQwwGXtA!SLdwTGqSfhwUo4CfbUJFeL7hETw64hOG8MQ0FK_o5YdnvVHaOp9Qxs4D7d5e10hj3YQ8EuaFc8qbnkynoP5dEA$ >> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!D9dNQwwGXtA!SLdwTGqSfhwUo4CfbUJFeL7hETw64hOG8MQ0FK_o5YdnvVHaOp9Qxs4D7d5e10hj3YQ8EuaFc8qbnkwuss43hA$ >> and provide commented, minimal, self-contained, reproducible code. >> >> -- >> * Andr?s Gonz?lez Carmona * >> > > [[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.Hello, Yes you can: subset(p, p %in% 1:20) # [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Hope this helps, Rui Barradas
@vi@e@gross m@iii@g oii gm@ii@com
2023-Feb-05 22:59 UTC
[R] Extracting data using subset function
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 at r-project.org> On Behalf Of Upananda Pani Sent: Sunday, February 5, 2023 2:33 PM To: Andr?s Gonz?lez Carmona <andresgc at ugr.es> Cc: r-help <r-help at 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 at 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 at 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 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.