[R] Extracting data using subset function

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Feb 5 23:17:23 CET 2023


Às 22:14 de 05/02/2023, Rui Barradas escreveu:
> À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 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_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 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.
> 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
> 
> ______________________________________________
> 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.
Hello,

But a much more natural the *first* n observations is with function head.


head(p, n = 20)
# [1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19


Hope this helps,

Rui Barradas



More information about the R-help mailing list