[R] work with a subset of the dataset

R. Michael Weylandt michael.weylandt at gmail.com
Fri Jan 13 05:08:10 CET 2012


Almost certainly ok here -- I have just seen too many instances where
the non-standard evaluation of subset() tripped someone up in a
programming context and figured it was better to get going in the `[`
direction now rather than introducing subset() into the OP's workflow.

Best,

Michael

On Thu, Jan 12, 2012 at 10:38 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
> Presuming that 'DF' is the data frame, I am not sure what is wrong with
>
>  NewDF <- subset(DF, (age >= 20) & (age <= 30))
>
> presuming that 20 and 30 are to be included.
>
> ?
>
> Marc Schwartz
>
> On Jan 12, 2012, at 9:26 PM, R. Michael Weylandt wrote:
>
>> You can probably do it more easily with the subset() function but in
>> my experience that often leads to more problems than solutions:
>> perhaps try this.
>>
>> idx <- with(DATA, which(age > 20 & age < 30))
>> DATA[idx, ]
>>
>> Michael
>>
>> On Thu, Jan 12, 2012 at 5:25 PM, manu79 <manuelespino79 at hotmail.it> wrote:
>>> Hello,
>>> I have a big dataset with many variables and I would like to consider only
>>> the rows in which there is a specific value of a variable.
>>>
>>> I make an example for explain what I mean:
>>> I have 5 variables describing a person: age, sex, weight, colour of hair,
>>> colour of eyes.
>>> I have 1000 rows (1000 persons) and I want to consider only the persons
>>> whose age is between 20 to 30. How can I do?
>>>
>>> Thank you very much
>>> M.
>



More information about the R-help mailing list