Defining categories

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Defining categories

bibek sharma
Hello R user,

Data below represent year in decimal. I would like to catagorize it
in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
catagory 2 and so on..
Any suggestion how it can be done with if else statement or any other way?

2.880556
0.616667
5.083333
0.858333
0.466667
2.936111
4.258333
0.258333
2.033333
2.583333
1.088889
0.447222
1.872222
0.080556
4.033333
4.116667
1.633333
2.147222

Thank you for  your help.
Bibek

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Defining categories

arun kirshna
Hi,
May be this:
dat1<-read.table(text="
2.880556
0.616667
5.083333
0.858333
0.466667
2.936111
4.258333
0.258333
2.033333
2.583333
1.088889
0.447222
1.872222
0.080556
4.033333
4.116667
1.633333
2.147222
",sep="",header=FALSE)
dat1$category<-ifelse(dat1$V1<=1 &dat1$V1>0,1,ifelse(dat1$V1>1 & dat1$V1<=2,2,ifelse(dat1$V1>2&dat1$V1<=3,3,ifelse(dat1$V1>3&dat1$V1<=4,4,ifelse(dat1$V1>4&dat1$V1<=5,5,6)))))


 head(dat1)
#        V1 category
#1 2.880556        3
#2 0.616667        1
#3 5.083333        6
#4 0.858333        1
#5 0.466667        1
#6 2.936111        3
A.K.



----- Original Message -----
From: bibek sharma <[hidden email]>
To: [hidden email]
Cc:
Sent: Wednesday, October 24, 2012 6:52 PM
Subject: [R] Defining categories

Hello R user,

Data below represent year in decimal. I would like to catagorize it
in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
catagory 2 and so on..
Any suggestion how it can be done with if else statement or any other way?

2.880556
0.616667
5.083333
0.858333
0.466667
2.936111
4.258333
0.258333
2.033333
2.583333
1.088889
0.447222
1.872222
0.080556
4.033333
4.116667
1.633333
2.147222

Thank you for  your help.
Bibek

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Defining categories

Jorge I Velez
See ?cut for a simpler way of doing this.
HTH,
Jorge.-


On Thu, Oct 25, 2012 at 10:02 AM, arun <> wrote:

> Hi,
> May be this:
> dat1<-read.table(text="
> 2.880556
> 0.616667
> 5.083333
> 0.858333
> 0.466667
> 2.936111
> 4.258333
> 0.258333
> 2.033333
> 2.583333
> 1.088889
> 0.447222
> 1.872222
> 0.080556
> 4.033333
> 4.116667
> 1.633333
> 2.147222
> ",sep="",header=FALSE)
> dat1$category<-ifelse(dat1$V1<=1 &dat1$V1>0,1,ifelse(dat1$V1>1 &
> dat1$V1<=2,2,ifelse(dat1$V1>2&dat1$V1<=3,3,ifelse(dat1$V1>3&dat1$V1<=4,4,ifelse(dat1$V1>4&dat1$V1<=5,5,6)))))
>
>
>  head(dat1)
> #        V1 category
> #1 2.880556        3
> #2 0.616667        1
> #3 5.083333        6
> #4 0.858333        1
> #5 0.466667        1
> #6 2.936111        3
> A.K.
>
>
>
> ----- Original Message -----
> From: bibek sharma <>
> To: [hidden email]
> Cc:
> Sent: Wednesday, October 24, 2012 6:52 PM
> Subject: [R] Defining categories
>
> Hello R user,
>
> Data below represent year in decimal. I would like to catagorize it
> in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
> catagory 2 and so on..
> Any suggestion how it can be done with if else statement or any other way?
>
> 2.880556
> 0.616667
> 5.083333
> 0.858333
> 0.466667
> 2.936111
> 4.258333
> 0.258333
> 2.033333
> 2.583333
> 1.088889
> 0.447222
> 1.872222
> 0.080556
> 4.033333
> 4.116667
> 1.633333
> 2.147222
>
> Thank you for  your help.
> Bibek
>
> ______________________________________________
> [hidden email] 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.
>
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Defining categories

arun kirshna
Hi,
 (Jorge: Thanks for the suggestion.)
cut? will be much easier.

dat1<-read.table(text="
2.880556
0.616667
5.083333
0.858333
0.466667
2.936111
4.258333
0.258333
2.033333
2.583333
1.088889
0.447222
1.872222
0.080556
4.033333
4.116667
1.633333
2.147222
",sep="",header=FALSE)
 dat1$Categ<-cut(dat1$V1,breaks=c(0,1,2,3,4,5,6))
#Either

library(car)
dat1$Categ<-recode(dat1$Categ,"'(0,1]'=1;'(1,2]'=2;'(2,3]'=3;'(3,4]'=4;'(4,5]'=5;'(5,6]'=6")
#or
dat1$Categ<-as.numeric(gsub(".*\\,(\\d+).*","\\1",dat1$Categ))
#formats the Categ column.
head(dat1)
#        V1 Categ
#1 2.880556     3
#2 0.616667     1
#3 5.083333     6
#4 0.858333     1
#5 0.466667     1
#6 2.936111     3
A.K.






________________________________
From: Jorge I Velez <[hidden email]>
To: arun <[hidden email]>
Cc: bibek sharma <[hidden email]>; R help <[hidden email]>
Sent: Wednesday, October 24, 2012 7:27 PM
Subject: Re: [R] Defining categories


See ?cut for a simpler way of doing this.
HTH,
Jorge.-



On Thu, Oct 25, 2012 at 10:02 AM, arun <> wrote:

Hi,

>May be this:
>dat1<-read.table(text="
>
>2.880556
>0.616667
>5.083333
>0.858333
>0.466667
>2.936111
>4.258333
>0.258333
>2.033333
>2.583333
>1.088889
>0.447222
>1.872222
>0.080556
>4.033333
>4.116667
>1.633333
>2.147222
>",sep="",header=FALSE)
>dat1$category<-ifelse(dat1$V1<=1 &dat1$V1>0,1,ifelse(dat1$V1>1 & dat1$V1<=2,2,ifelse(dat1$V1>2&dat1$V1<=3,3,ifelse(dat1$V1>3&dat1$V1<=4,4,ifelse(dat1$V1>4&dat1$V1<=5,5,6)))))
>
>
> head(dat1)
>#        V1 category
>#1 2.880556        3
>#2 0.616667        1
>#3 5.083333        6
>#4 0.858333        1
>#5 0.466667        1
>#6 2.936111        3
>A.K.
>
>
>
>
>----- Original Message -----
>From: bibek sharma <>
>To: [hidden email]
>Cc:
>Sent: Wednesday, October 24, 2012 6:52 PM
>Subject: [R] Defining categories
>
>Hello R user,
>
>Data below represent year in decimal. I would like to catagorize it
>in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
>catagory 2 and so on..
>Any suggestion how it can be done with if else statement or any other way?
>
>2.880556
>0.616667
>5.083333
>0.858333
>0.466667
>2.936111
>4.258333
>0.258333
>2.033333
>2.583333
>1.088889
>0.447222
>1.872222
>0.080556
>4.033333
>4.116667
>1.633333
>2.147222
>
>Thank you for  your help.
>Bibek
>
>______________________________________________
>[hidden email] 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.
>
>
>______________________________________________
>[hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Defining categories

PIKAL Petr
Hi

Maybe also findInterval can be used

dat1$cat<-findInterval(dat1$V1, 1:6, rightmost.closed = T, all.inside = T)

It is said to be more efficient than cut.

Regards
Petr



> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of arun
> Sent: Thursday, October 25, 2012 3:32 AM
> To: Jorge I Velez; bibek sharma
> Cc: R help
> Subject: Re: [R] Defining categories
>
> Hi,
>  (Jorge: Thanks for the suggestion.)
> cut? will be much easier.
>
> dat1<-read.table(text="
> 2.880556
> 0.616667
> 5.083333
> 0.858333
> 0.466667
> 2.936111
> 4.258333
> 0.258333
> 2.033333
> 2.583333
> 1.088889
> 0.447222
> 1.872222
> 0.080556
> 4.033333
> 4.116667
> 1.633333
> 2.147222
> ",sep="",header=FALSE)
>  dat1$Categ<-cut(dat1$V1,breaks=c(0,1,2,3,4,5,6))
> #Either
>
> library(car)
> dat1$Categ<-
> recode(dat1$Categ,"'(0,1]'=1;'(1,2]'=2;'(2,3]'=3;'(3,4]'=4;'(4,5]'=5;'(
> 5,6]'=6")
> #or
> dat1$Categ<-as.numeric(gsub(".*\\,(\\d+).*","\\1",dat1$Categ))
> #formats the Categ column.
> head(dat1)
> #        V1 Categ
> #1 2.880556     3
> #2 0.616667     1
> #3 5.083333     6
> #4 0.858333     1
> #5 0.466667     1
> #6 2.936111     3
> A.K.
>
>
>
>
>
>
> ________________________________
> From: Jorge I Velez <[hidden email]>
> To: arun <[hidden email]>
> Cc: bibek sharma <[hidden email]>; R help <[hidden email]>
> Sent: Wednesday, October 24, 2012 7:27 PM
> Subject: Re: [R] Defining categories
>
>
> See ?cut for a simpler way of doing this.
> HTH,
> Jorge.-
>
>
>
> On Thu, Oct 25, 2012 at 10:02 AM, arun <> wrote:
>
> Hi,
> >May be this:
> >dat1<-read.table(text="
> >
> >2.880556
> >0.616667
> >5.083333
> >0.858333
> >0.466667
> >2.936111
> >4.258333
> >0.258333
> >2.033333
> >2.583333
> >1.088889
> >0.447222
> >1.872222
> >0.080556
> >4.033333
> >4.116667
> >1.633333
> >2.147222
> >",sep="",header=FALSE)
> >dat1$category<-ifelse(dat1$V1<=1 &dat1$V1>0,1,ifelse(dat1$V1>1 &
> dat1$V1<=2,2,ifelse(dat1$V1>2&dat1$V1<=3,3,ifelse(dat1$V1>3&dat1$V1<=4,
> 4,ifelse(dat1$V1>4&dat1$V1<=5,5,6)))))
> >
> >
> > head(dat1)
> >#        V1 category
> >#1 2.880556        3
> >#2 0.616667        1
> >#3 5.083333        6
> >#4 0.858333        1
> >#5 0.466667        1
> >#6 2.936111        3
> >A.K.
> >
> >
> >
> >
> >----- Original Message -----
> >From: bibek sharma <>
> >To: [hidden email]
> >Cc:
> >Sent: Wednesday, October 24, 2012 6:52 PM
> >Subject: [R] Defining categories
> >
> >Hello R user,
> >
> >Data below represent year in decimal. I would like to catagorize it
> >in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
> >catagory 2 and so on..
> >Any suggestion how it can be done with if else statement or any other
> way?
> >
> >2.880556
> >0.616667
> >5.083333
> >0.858333
> >0.466667
> >2.936111
> >4.258333
> >0.258333
> >2.033333
> >2.583333
> >1.088889
> >0.447222
> >1.872222
> >0.080556
> >4.033333
> >4.116667
> >1.633333
> >2.147222
> >
> >Thank you for  your help.
> >Bibek
> >
> >______________________________________________
> >[hidden email] 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.
> >
> >
> >______________________________________________
> >[hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Defining categories

Jorge I Velez
Thank you Dr. Pikal for this alternative.
Best,
Jorge.-

Sent from my phone. Please excuse my brevity and misspelling.

On Oct 25, 2012, at 8:29 PM, PIKAL Petr <[hidden email]> wrote:

> Hi
>
> Maybe also findInterval can be used
>
> dat1$cat<-findInterval(dat1$V1, 1:6, rightmost.closed = T, all.inside = T)
>
> It is said to be more efficient than cut.
>
> Regards
> Petr
>
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:r-help-bounces@r-
>> project.org] On Behalf Of arun
>> Sent: Thursday, October 25, 2012 3:32 AM
>> To: Jorge I Velez; bibek sharma
>> Cc: R help
>> Subject: Re: [R] Defining categories
>>
>> Hi,
>>  (Jorge: Thanks for the suggestion.)
>> cut? will be much easier.
>>
>> dat1<-read.table(text="
>> 2.880556
>> 0.616667
>> 5.083333
>> 0.858333
>> 0.466667
>> 2.936111
>> 4.258333
>> 0.258333
>> 2.033333
>> 2.583333
>> 1.088889
>> 0.447222
>> 1.872222
>> 0.080556
>> 4.033333
>> 4.116667
>> 1.633333
>> 2.147222
>> ",sep="",header=FALSE)
>>  dat1$Categ<-cut(dat1$V1,breaks=c(0,1,2,3,4,5,6))
>> #Either
>>
>> library(car)
>> dat1$Categ<-
>> recode(dat1$Categ,"'(0,1]'=1;'(1,2]'=2;'(2,3]'=3;'(3,4]'=4;'(4,5]'=5;'(
>> 5,6]'=6")
>> #or
>> dat1$Categ<-as.numeric(gsub(".*\\,(\\d+).*","\\1",dat1$Categ))
>> #formats the Categ column.
>> head(dat1)
>> #        V1 Categ
>> #1 2.880556     3
>> #2 0.616667     1
>> #3 5.083333     6
>> #4 0.858333     1
>> #5 0.466667     1
>> #6 2.936111     3
>> A.K.
>>
>>
>>
>>
>>
>>
>> ________________________________
>> From: Jorge I Velez <[hidden email]>
>> To: arun <[hidden email]>
>> Cc: bibek sharma <[hidden email]>; R help <[hidden email]>
>> Sent: Wednesday, October 24, 2012 7:27 PM
>> Subject: Re: [R] Defining categories
>>
>>
>> See ?cut for a simpler way of doing this.
>> HTH,
>> Jorge.-
>>
>>
>>
>> On Thu, Oct 25, 2012 at 10:02 AM, arun <> wrote:
>>
>> Hi,
>>> May be this:
>>> dat1<-read.table(text="
>>>
>>> 2.880556
>>> 0.616667
>>> 5.083333
>>> 0.858333
>>> 0.466667
>>> 2.936111
>>> 4.258333
>>> 0.258333
>>> 2.033333
>>> 2.583333
>>> 1.088889
>>> 0.447222
>>> 1.872222
>>> 0.080556
>>> 4.033333
>>> 4.116667
>>> 1.633333
>>> 2.147222
>>> ",sep="",header=FALSE)
>>> dat1$category<-ifelse(dat1$V1<=1 &dat1$V1>0,1,ifelse(dat1$V1>1 &
>> dat1$V1<=2,2,ifelse(dat1$V1>2&dat1$V1<=3,3,ifelse(dat1$V1>3&dat1$V1<=4,
>> 4,ifelse(dat1$V1>4&dat1$V1<=5,5,6)))))
>>>
>>>
>>>  head(dat1)
>>> #        V1 category
>>> #1 2.880556        3
>>> #2 0.616667        1
>>> #3 5.083333        6
>>> #4 0.858333        1
>>> #5 0.466667        1
>>> #6 2.936111        3
>>> A.K.
>>>
>>>
>>>
>>>
>>> ----- Original Message -----
>>> From: bibek sharma <>
>>> To: [hidden email]
>>> Cc:
>>> Sent: Wednesday, October 24, 2012 6:52 PM
>>> Subject: [R] Defining categories
>>>
>>> Hello R user,
>>>
>>> Data below represent year in decimal. I would like to catagorize it
>>> in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
>>> catagory 2 and so on..
>>> Any suggestion how it can be done with if else statement or any other
>> way?
>>>
>>> 2.880556
>>> 0.616667
>>> 5.083333
>>> 0.858333
>>> 0.466667
>>> 2.936111
>>> 4.258333
>>> 0.258333
>>> 2.033333
>>> 2.583333
>>> 1.088889
>>> 0.447222
>>> 1.872222
>>> 0.080556
>>> 4.033333
>>> 4.116667
>>> 1.633333
>>> 2.147222
>>>
>>> Thank you for  your help.
>>> Bibek
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Defining categories

arun kirshna
In reply to this post by PIKAL Petr
HI Petr,

Thanks for sharing the function.  True, very efficient than cut.

dat1$cat<-findInterval(dat1$V1, 1:6, rightmost.closed = T, all.inside = T)
                                                  ^^^
 #  May be the interval is 0:6.
dat1$cat1<-findInterval(dat1$V1, 1:6, rightmost.closed = T, all.inside = T)
 dat1$cat2<-findInterval(dat1$V1, 0:6, rightmost.closed = T, all.inside = T)
 head(dat1)
#        V1 cat1 cat2
#1 2.880556    2    3
#2 0.616667    1    1
#3 5.083333    5    6
#4 0.858333    1    1
#5 0.466667    1    1
#6 2.936111    2    3
A.K.




----- Original Message -----
From: PIKAL Petr <[hidden email]>
To: arun <[hidden email]>; Jorge I Velez <[hidden email]>; bibek sharma <[hidden email]>
Cc: R help <[hidden email]>
Sent: Thursday, October 25, 2012 5:29 AM
Subject: RE: [R] Defining categories

Hi

Maybe also findInterval can be used

dat1$cat<-findInterval(dat1$V1, 1:6, rightmost.closed = T, all.inside = T)

It is said to be more efficient than cut.

Regards
Petr



> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of arun
> Sent: Thursday, October 25, 2012 3:32 AM
> To: Jorge I Velez; bibek sharma
> Cc: R help
> Subject: Re: [R] Defining categories
>
> Hi,
>  (Jorge: Thanks for the suggestion.)
> cut? will be much easier.
>
> dat1<-read.table(text="
> 2.880556
> 0.616667
> 5.083333
> 0.858333
> 0.466667
> 2.936111
> 4.258333
> 0.258333
> 2.033333
> 2.583333
> 1.088889
> 0.447222
> 1.872222
> 0.080556
> 4.033333
> 4.116667
> 1.633333
> 2.147222
> ",sep="",header=FALSE)
>  dat1$Categ<-cut(dat1$V1,breaks=c(0,1,2,3,4,5,6))
> #Either
>
> library(car)
> dat1$Categ<-
> recode(dat1$Categ,"'(0,1]'=1;'(1,2]'=2;'(2,3]'=3;'(3,4]'=4;'(4,5]'=5;'(
> 5,6]'=6")
> #or
> dat1$Categ<-as.numeric(gsub(".*\\,(\\d+).*","\\1",dat1$Categ))
> #formats the Categ column.
> head(dat1)
> #        V1 Categ
> #1 2.880556     3
> #2 0.616667     1
> #3 5.083333     6
> #4 0.858333     1
> #5 0.466667     1
> #6 2.936111     3
> A.K.
>
>
>
>
>
>
> ________________________________
> From: Jorge I Velez <[hidden email]>
> To: arun <[hidden email]>
> Cc: bibek sharma <[hidden email]>; R help <[hidden email]>
> Sent: Wednesday, October 24, 2012 7:27 PM
> Subject: Re: [R] Defining categories
>
>
> See ?cut for a simpler way of doing this.
> HTH,
> Jorge.-
>
>
>
> On Thu, Oct 25, 2012 at 10:02 AM, arun <> wrote:
>
> Hi,
> >May be this:
> >dat1<-read.table(text="
> >
> >2.880556
> >0.616667
> >5.083333
> >0.858333
> >0.466667
> >2.936111
> >4.258333
> >0.258333
> >2.033333
> >2.583333
> >1.088889
> >0.447222
> >1.872222
> >0.080556
> >4.033333
> >4.116667
> >1.633333
> >2.147222
> >",sep="",header=FALSE)
> >dat1$category<-ifelse(dat1$V1<=1 &dat1$V1>0,1,ifelse(dat1$V1>1 &
> dat1$V1<=2,2,ifelse(dat1$V1>2&dat1$V1<=3,3,ifelse(dat1$V1>3&dat1$V1<=4,
> 4,ifelse(dat1$V1>4&dat1$V1<=5,5,6)))))
> >
> >
> > head(dat1)
> >#        V1 category
> >#1 2.880556        3
> >#2 0.616667        1
> >#3 5.083333        6
> >#4 0.858333        1
> >#5 0.466667        1
> >#6 2.936111        3
> >A.K.
> >
> >
> >
> >
> >----- Original Message -----
> >From: bibek sharma <>
> >To: [hidden email]
> >Cc:
> >Sent: Wednesday, October 24, 2012 6:52 PM
> >Subject: [R] Defining categories
> >
> >Hello R user,
> >
> >Data below represent year in decimal. I would like to catagorize it
> >in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
> >catagory 2 and so on..
> >Any suggestion how it can be done with if else statement or any other
> way?
> >
> >2.880556
> >0.616667
> >5.083333
> >0.858333
> >0.466667
> >2.936111
> >4.258333
> >0.258333
> >2.033333
> >2.583333
> >1.088889
> >0.447222
> >1.872222
> >0.080556
> >4.033333
> >4.116667
> >1.633333
> >2.147222
> >
> >Thank you for  your help.
> >Bibek
> >
> >______________________________________________
> >[hidden email] 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.
> >
> >
> >______________________________________________
> >[hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Defining categories

bibek sharma
Thank you all for your suggestions. It really helped me a lot.
Best,
Bibek

On Thu, Oct 25, 2012 at 4:44 AM, arun <[hidden email]> wrote:

> HI Petr,
>
> Thanks for sharing the function.  True, very efficient than cut.
>
> dat1$cat<-findInterval(dat1$V1, 1:6, rightmost.closed = T, all.inside = T)
>                                                   ^^^
>  #  May be the interval is 0:6.
> dat1$cat1<-findInterval(dat1$V1, 1:6, rightmost.closed = T, all.inside = T)
>  dat1$cat2<-findInterval(dat1$V1, 0:6, rightmost.closed = T, all.inside = T)
>  head(dat1)
> #        V1 cat1 cat2
> #1 2.880556    2    3
> #2 0.616667    1    1
> #3 5.083333    5    6
> #4 0.858333    1    1
> #5 0.466667    1    1
> #6 2.936111    2    3
> A.K.
>
>
>
>
> ----- Original Message -----
> From: PIKAL Petr <[hidden email]>
> To: arun <[hidden email]>; Jorge I Velez <[hidden email]>; bibek sharma <[hidden email]>
> Cc: R help <[hidden email]>
> Sent: Thursday, October 25, 2012 5:29 AM
> Subject: RE: [R] Defining categories
>
> Hi
>
> Maybe also findInterval can be used
>
> dat1$cat<-findInterval(dat1$V1, 1:6, rightmost.closed = T, all.inside = T)
>
> It is said to be more efficient than cut.
>
> Regards
> Petr
>
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:r-help-bounces@r-
>> project.org] On Behalf Of arun
>> Sent: Thursday, October 25, 2012 3:32 AM
>> To: Jorge I Velez; bibek sharma
>> Cc: R help
>> Subject: Re: [R] Defining categories
>>
>> Hi,
>>  (Jorge: Thanks for the suggestion.)
>> cut? will be much easier.
>>
>> dat1<-read.table(text="
>> 2.880556
>> 0.616667
>> 5.083333
>> 0.858333
>> 0.466667
>> 2.936111
>> 4.258333
>> 0.258333
>> 2.033333
>> 2.583333
>> 1.088889
>> 0.447222
>> 1.872222
>> 0.080556
>> 4.033333
>> 4.116667
>> 1.633333
>> 2.147222
>> ",sep="",header=FALSE)
>>  dat1$Categ<-cut(dat1$V1,breaks=c(0,1,2,3,4,5,6))
>> #Either
>>
>> library(car)
>> dat1$Categ<-
>> recode(dat1$Categ,"'(0,1]'=1;'(1,2]'=2;'(2,3]'=3;'(3,4]'=4;'(4,5]'=5;'(
>> 5,6]'=6")
>> #or
>> dat1$Categ<-as.numeric(gsub(".*\\,(\\d+).*","\\1",dat1$Categ))
>> #formats the Categ column.
>> head(dat1)
>> #        V1 Categ
>> #1 2.880556     3
>> #2 0.616667     1
>> #3 5.083333     6
>> #4 0.858333     1
>> #5 0.466667     1
>> #6 2.936111     3
>> A.K.
>>
>>
>>
>>
>>
>>
>> ________________________________
>> From: Jorge I Velez <[hidden email]>
>> To: arun <[hidden email]>
>> Cc: bibek sharma <[hidden email]>; R help <[hidden email]>
>> Sent: Wednesday, October 24, 2012 7:27 PM
>> Subject: Re: [R] Defining categories
>>
>>
>> See ?cut for a simpler way of doing this.
>> HTH,
>> Jorge.-
>>
>>
>>
>> On Thu, Oct 25, 2012 at 10:02 AM, arun <> wrote:
>>
>> Hi,
>> >May be this:
>> >dat1<-read.table(text="
>> >
>> >2.880556
>> >0.616667
>> >5.083333
>> >0.858333
>> >0.466667
>> >2.936111
>> >4.258333
>> >0.258333
>> >2.033333
>> >2.583333
>> >1.088889
>> >0.447222
>> >1.872222
>> >0.080556
>> >4.033333
>> >4.116667
>> >1.633333
>> >2.147222
>> >",sep="",header=FALSE)
>> >dat1$category<-ifelse(dat1$V1<=1 &dat1$V1>0,1,ifelse(dat1$V1>1 &
>> dat1$V1<=2,2,ifelse(dat1$V1>2&dat1$V1<=3,3,ifelse(dat1$V1>3&dat1$V1<=4,
>> 4,ifelse(dat1$V1>4&dat1$V1<=5,5,6)))))
>> >
>> >
>> > head(dat1)
>> >#        V1 category
>> >#1 2.880556        3
>> >#2 0.616667        1
>> >#3 5.083333        6
>> >#4 0.858333        1
>> >#5 0.466667        1
>> >#6 2.936111        3
>> >A.K.
>> >
>> >
>> >
>> >
>> >----- Original Message -----
>> >From: bibek sharma <>
>> >To: [hidden email]
>> >Cc:
>> >Sent: Wednesday, October 24, 2012 6:52 PM
>> >Subject: [R] Defining categories
>> >
>> >Hello R user,
>> >
>> >Data below represent year in decimal. I would like to catagorize it
>> >in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
>> >catagory 2 and so on..
>> >Any suggestion how it can be done with if else statement or any other
>> way?
>> >
>> >2.880556
>> >0.616667
>> >5.083333
>> >0.858333
>> >0.466667
>> >2.936111
>> >4.258333
>> >0.258333
>> >2.033333
>> >2.583333
>> >1.088889
>> >0.447222
>> >1.872222
>> >0.080556
>> >4.033333
>> >4.116667
>> >1.633333
>> >2.147222
>> >
>> >Thank you for  your help.
>> >Bibek
>> >
>> >______________________________________________
>> >[hidden email] 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.
>> >
>> >
>> >______________________________________________
>> >[hidden email] 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.
>> >
>>
>> ______________________________________________
>> [hidden email] 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.
>

______________________________________________
[hidden email] 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.