Skip to content

cut data into sevral group and assign calculated values individually

4 messages · Li, Yan, Uwe Ligges, PIKAL Petr

#
On 17.10.2011 20:53, Li, Yan wrote:
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
#
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:
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
1 day later
#
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
it:
NA
http://www.R-project.org/posting-guide.html
http://www.R-project.org/posting-guide.html