[R] how to select cases based on value of one or more variables
Simone Gabbriellini
simone.gabbriellini at gmail.com
Sun Nov 30 22:56:41 CET 2008
yes it's THE solution!
thank you very much,
Simone
Il giorno 30/nov/08, alle ore 22:42, Kingsford Jones ha scritto:
> It's generally easier to work with data frames, so read your data with
>
> students <- read.spss(yourFile, to.data.frame=TRUE)
>
>
> Then subset will work as expected:
>
> subset(students, Sex == 1)
>
>
> If you would rather keep the data as a list you could do something
> like
>
> lapply(students, function(x) x[students$Sex == 1])
>
> hth,
>
> Kingsford Jones
>
>
>
> On Sun, Nov 30, 2008 at 2:15 PM, Simone Gabbriellini
> <simone.gabbriellini at gmail.com> wrote:
>> sorry for my bad presentation...
>>
>> read.spss gives me this:
>>
>>> students
>> $Auno
>> [1] 6 1 2 2 1 3 4 2 4 2 4 4 1 1 NA 1 4 2 1 1 1
>> 5 4
>> [24] 2 1 2 1 2 1 4 4 1 1 1 2 1 6 1 1 1 1 1 2
>> 1 2 1
>> [47] 2 2 1 4 2 4 3 1 1 1 1 3 2 1 4 4 4 4 2 4
>> 1 2 4
>> [70] 1 3 4 5 2 4 3 5 5 4 2 1 1 1 1 4 5 2 4 4
>> 1 4 2
>> [93] 1 2 3 3 2 1 2 2 2 1 1 1 3 5 5 5 2 NA 2 1
>> NA 5 2
>> [116] 1 4 2 NA 1 4 5 2 3 1 1 1 1 4 2 1 1 3 2 4
>> 2 4 2
>> [139] 1 4 1 2 4 1 2 3 2 1 1 2 4 4 3 4 1 1 3 2
>> 1 1 2
>> [162] 1 2 5 5 5 1 4 3 2 3 3 2 1 1 5 1 2 1 1 2
>> 1 2 1
>> [185] 1 2 1 1 1 1 3 4 2 1 4 2 4 1 4 2 1 1 1 2
>> 1 4 1
>> [208] 5 1 1 4 4 2 1 1 5 4 1 1 5 5 4 1 4
>>
>> $Sex
>> [1] 2 1 2 1 2 2 2 2 1 2 1 1 2 1 0 2 2 2 2 2 2 2 2 1 2 2 1 2 2 1 2 2
>> 2 1
>> [35] 2 2 2 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 2 2 2 2 2 2 2 1 1 2 2 2 2
>> 2 1 2
>> [69] 2 1 2 1 2 1 2 2 2 2 2 2 1 2 2 2 1 2 2 2 1 1 1 2 1 2 1 2 2 2 2
>> 2 2 2
>> [103] 2 2 2 2 1 2 2 2 2 2 2 2 2 2 1 2 0 2 2 2 1 2 2 1 2 1 2 2 1 1 2
>> 1 2 1
>> [137] 2 1 2 1 1 1 1 1 1 2 1 1 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 1
>> 1 2 2
>> [171] 2 2 2 2 2 1 1 1 1 2 2 2 2 2 2 2 2 1 2 2 1 1 1 1 1 2 0 2 2 1 2
>> 1 2 2
>> [205] 1 2 2 2 2 2 2 2 2 2 1 2 2 1 1 2 2 2 2 2
>>
>> ....
>>
>> I would like to filter - or subset - the dataset for $Sex = 1 (in
>> this case
>> means male...), for example...
>>
>> thanks anyway,
>> Simone
>>
>>
>>
>> Il giorno 30/nov/08, alle ore 21:49, Don MacQueen ha scritto:
>>
>>> It is.
>>>
>>> For example, if you have a variable stored as a vector named "x",
>>> and
>>> another variable stored as aa vector named "y", you can select
>>> cases of y
>>> where x is greater than 3 by using
>>>
>>> y[x>3]
>>>
>>> However, you're going to have to provide more information in order
>>> to get
>>> a better answer than that (see the posting guide, link included
>>> with every
>>> post to r-help). In particular, I'm guessing that the answer you
>>> really want
>>> looks somewhat different than my example -- but this depends on
>>> the exact
>>> structure of what read.spss() produces.
>>>
>>> I'd also suggest reading some of the documentation available from
>>> the R
>>> website (CRAN), notably, "An Introduction to R".
>>>
>>> -Don
>>>
>>> At 9:36 PM +0100 11/30/08, Simone Gabbriellini wrote:
>>>>
>>>> dear list,
>>>>
>>>> I have read a spss file with read.spss()
>>>>
>>>> now I have a list with all my variable stored as vectors.
>>>>
>>>> is it possible to selec cases based on the value of one or more
>>>> variables?
>>>>
>>>> thank you,
>>>> Simone
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> 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.
>>>
>>>
>>> --
>>> ---------------------------------
>>> Don MacQueen
>>> Lawrence Livermore National Laboratory
>>> Livermore, CA, USA
>>> 925-423-1062
>>> macq at llnl.gov
>>> ---------------------------------
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>>
More information about the R-help
mailing list