[R] Data indexing issue...

jim holtman jholtman at gmail.com
Tue Mar 27 13:58:37 CEST 2012


Why not use 'split' and get all the groups at once:

result <- split(Calandra, list(Calandra$Day, Calandra$Season, drop = TRUE)

On Tue, Mar 27, 2012 at 7:43 AM, Ivan Calandra
<ivan.calandra at u-bourgogne.fr> wrote:
> Hi HJ,
>
> Take a look at ?"&"; this is probably what you're looking for.
>
> What you could also do is:
> Calender[Calender$Day=='Wd' & Calender$Season=="Winter", ]  # notice the
> last comma
>
> This will subset directly without using which(); it might be helpful to you.
>
> HTH,
> Ivan
>
> --
> Ivan CALANDRA
> Université de Bourgogne
> UMR CNRS/uB 6282 Biogéosciences
> 6 Boulevard Gabriel
> 21000 Dijon, FRANCE
> +33(0)3.80.39.63.06
> ivan.calandra at u-bourgogne.fr
> http://biogeosciences.u-bourgogne.fr/calandra
>
>
> Le 27/03/12 12:32, HJ YAN a écrit :
>
>> Dear R-help,
>>
>> My dataset (which is a data frame, called 'Calender' here)  includes 365
>> rows representing 365 days for a year.  One column ('Season')contains
>> factor data representing seasons, e.g. spring, summer, autumn and winter.
>> Another column (called 'Day') contains data representing wether the day
>>  is
>> a working day  (I use 'Wd' for short here)or weekend (I use 'Wkend' for
>> short here).
>>
>>
>> I want to seperate the index of the working days  and weekends for each
>> season. I used R commend "which" before for one criteria, for example, if
>> I
>> use...
>>
>>
>> WdIndex<-which(Calender$Day=='Wd')
>>
>> that will gives a set of indeices of working days in the year.
>>
>> I wonder in R could I use a combination of something such as 'AND' , 'OR'
>> (e.g. in MySQL) to set 'multi-criteria'  when selecting data. So for
>> example...
>>
>> WinterWdIndex<-which(Calender$Day=='Wd' AND Calender$Season=="Winter")
>>
>>
>> I know the above syntax is wrong, and I checked '?which' which did not
>> give
>> me an answer and also tried '?AND' but seems it doesn`t exist at all...
>>
>>
>> Many thanks!
>> HJ
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>>
>
> ______________________________________________
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list