Re: [R] Create a categorical variable using the deciles of data

2022-06-15 Thread Richard O'Keefe
I had the advantage of studying
   Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
   "The New S Language".  Wadsworth & Brooks/Cole.
before starting to use R.  That book was reissued by CRC Press
only a few years ago.  It's *still* a pretty darned good intro
to R.  cut, pretty, and quantile are all there.  They are
pretty basic.

I strongly recommend looking for a copy of that book and
skimming through appendix 1 repeatedly until you have a rough
idea of what's there.  A lot has been added since then, and R
never did srarch the current working directory as part of the
environment, but a lot has NOT changed.  category(..) has gone,
that's the main difference I recall.


On Wed, 15 Jun 2022 at 01:49, Ebert,Timothy Aaron  wrote:

> A problem in R is that there are several dozen ways to do any of these
> basic activities. I used the approach that I could get to work the fastest
> and tried to make it somewhat general. I do not know functions like ?pretty
> that Rui used, nor ?quantile or ?cut. It is a difficulty in learning R
> where the internet or sites like this one are the "teacher." There are
> plenty of books, but they too take one approach to solve a problem rather
> than "here is a problem" and "these are all possible solutions." I
> appreciate seeing alternative solutions.
>
> Tim
>
> -Original Message-
> From: R-help  On Behalf Of Richard O'Keefe
> Sent: Tuesday, June 14, 2022 9:08 AM
> To: anteneh asmare 
> Cc: R Project Help 
> Subject: Re: [R] Create a categorical variable using the deciles of data
>
> [External Email]
>
> Can you explain why you are not using
> ?quantile
> to find the deciles then
> ?cut
> to construct the factor?
> What have I misunderstood?
>
> On Tue, 14 Jun 2022 at 23:29, anteneh asmare  wrote:
>
> > I want Create a categorical variable using the deciles of the
> > following data frame to divide the individuals into 10 groups equally.
> > I try the following codes
> > data_catigocal<-data.frame(c(1:5))
> > # create categorical vector using deciles group_vector <-
> >
> > c('0-10','11-20','21-30','31-40','41-50','51-60','61-70','71-80','81-9
> > 0','91-100') # Add categorical variable to the data_catigocal
> > data_catigocal$decile <- factor(group_vector) # print data frame
> > data_catigocal
> >
> > can any one help me with the r code
> > Kind regards,
> > Hana
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mail
> > man_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAs
> > Rzsn7AkP-g=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotO
> > cQ44yzy=2x4gMg5K_GJPK-XUk3UfSB3hhFCziCOgqvxl7yJXTvA=
> > PLEASE do read the posting guide
> > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or
> > g_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeA
> > sRzsn7AkP-g=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuot
> > OcQ44yzy=50k59quZy2KmFsVBRxK4P-M7RyxDsPGieX6TiiY5or0=
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotOcQ44yzy=2x4gMg5K_GJPK-XUk3UfSB3hhFCziCOgqvxl7yJXTvA=
> PLEASE do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotOcQ44yzy=50k59quZy2KmFsVBRxK4P-M7RyxDsPGieX6TiiY5or0=
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Create a categorical variable using the deciles of data

2022-06-14 Thread Ebert,Timothy Aaron
Hana, the "right" answer depends on exactly what you need. Here are three 
correct solutions. They use the same basic strategy to give different results. 
There are also other approaches in R to get the same outcome. You could use 
data_catigocal[i,j] and some for loops. 

size1 <-5
ngroup <- 10 # note that size1 must be evenly divisible by ngroup
group_size <- size1/ngroup
data_catigocal <-data.frame(c(1:size1))
data_categorical1<-data_catigocal
# create categorical vector using deciles 
group_vector <- 
c('0-10','11-20','21-30','31-40','41-50','51-60','61-70','71-80','81-90','91-100')
data_categorical1$group_vn <-rep(group_vector,group_size)

option2 <- rep(group_vector, group_size)
option2 <- sort(option2, decreasing=FALSE)
data_categorical2 <- cbind(option2, data_catigocal)

option3 <- rep(group_vector, group_size)
option3a <- sample(option3, size1, replace=FALSE)
data_categorical3 <- cbind(option3a, data_catigocal)



Tim

-Original Message-
From: R-help  On Behalf Of anteneh asmare
Sent: Tuesday, June 14, 2022 7:29 AM
To: r-help@r-project.org
Subject: [R] Create a categorical variable using the deciles of data

[External Email]

I want Create a categorical variable using the deciles of the following data 
frame to divide the individuals into 10 groups equally.
I try the following codes
data_catigocal<-data.frame(c(1:5))
# create categorical vector using deciles group_vector <-
c('0-10','11-20','21-30','31-40','41-50','51-60','61-70','71-80','81-90','91-100')
# Add categorical variable to the data_catigocal data_catigocal$decile <- 
factor(group_vector) # print data frame data_catigocal

can any one help me with the r code
Kind regards,
Hana

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=iJ1M9ZDgTrZDuxyw_CUg03Mb6JmtrOaSF0JqAl-1pdmgbKG3AWiI6hMbv9LVOjKN=eUb_8T4KZRbFW_poDuhkWwPvNKQdkI6fm0MMTsOyh-A=
PLEASE do read the posting guide 
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=iJ1M9ZDgTrZDuxyw_CUg03Mb6JmtrOaSF0JqAl-1pdmgbKG3AWiI6hMbv9LVOjKN=tnk4qRX6T6SZuapvkrNEZOtHmOVlKGS-02yHEzajqS8=
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.