[R] cut data into sevral group and assign calculated values individually

Petr PIKAL petr.pikal at precheza.cz
Wed Oct 19 16:32:08 CEST 2011


Hm

Not much of an example.

You can cut your m according to breaks

cc <- cut(m, c(0,331,476, 608, 791, 900))
[1] (0,331]   (331,476] (476,608] (608,791] (791,900] <NA> 
Levels: (0,331] (331,476] (476,608] (608,791] (791,900]

You can add NA as an extra level by

factor(cc, exclude=NULL)
[1] (0,331]   (331,476] (476,608] (608,791] (791,900] <NA> 
Levels: (0,331] (331,476] (476,608] (608,791] (791,900] <NA>

but I wonder what you want to do with m_group then.

You shall explain in more detail what you want to do to get sensible 
answer.

Regards
Petr

> 
> Thanks for your reply.
> 
> Let me make an example then:
> 
> m<- c(150, 400, 500,750,800, NA)
> 
> How can I use cut to generate the m_group as c(0,0.4755,1, 0.2275,0,0):
> 
> Breaks                  331.04      476.07      608.66       791.5    NA
> m_group        0                 x          1           x 0         0
> 
> Thank you very much!
> 
> Regards,
> Yan
> 
> -----Original Message-----
> From: Uwe Ligges [mailto:ligges at statistik.tu-dortmund.de] 
> Sent: Tuesday, October 18, 2011 5:23 AM
> To: Li, Yan
> Cc: r-help at r-project.org
> Subject: Re: [R] cut data into sevral group and assign calculated values 

> individually
> 
> 
> 
> On 17.10.2011 20:53, Li, Yan wrote:
> > Hi All,
> >
> > I have some data from which I set four points to be breaks. Based on 
> these points,  I cut the dataset into four groups and assign a number to 
it:
> >
> > <=331.04 assign 0
> >> 331.04<=476.07 assign data-331.04/(476.07-331.04)
> >> 476.07<=608.66  assign 1
> >> 608.66<=791.5 assign 791.5- data/(791.5-608.66)
> >> 791.5 and NA assign 0
> >
> >
> > Breaks                  331.04      476.07      608.66       791.5    
NA
> > m_group        0                 x                 1                 x 
 
> 0                    0
> >
> >
> > I can use cut() to group the data according to the breaks but having 
> difficulty to assign the two calculated interval values.
> >
> 
> 
> Based on the levels of the factor resulted by cut(), you can calculate a 

> new vector easily. But since you have not specified a reproducible 
> example, I cannot quickly change it in order to show how it works.
> 
> Best,
> Uwe Ligges
> 
> 
> 
> > Thank you very much!
> >
> > Regards,
> > Yan
> >
> >    [[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.



More information about the R-help mailing list