Ebert,Timothy Aaron
2022-Jun-14 13:49 UTC
[R] Create a categorical variable using the deciles of data
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 <r-help-bounces at r-project.org> On Behalf Of Richard O'Keefe Sent: Tuesday, June 14, 2022 9:08 AM To: anteneh asmare <hanatezera at gmail.com> Cc: R Project Help <r-help at r-project.org> 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 <hanatezera at gmail.com> 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:50000)) > # 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 at 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&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAs > Rzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotO > cQ44yzy&s=2x4gMg5K_GJPK-XUk3UfSB3hhFCziCOgqvxl7yJXTvA&e> PLEASE do read the posting guide > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or > g_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA > sRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuot > OcQ44yzy&s=50k59quZy2KmFsVBRxK4P-M7RyxDsPGieX6TiiY5or0&e> and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]] ______________________________________________ R-help at 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&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotOcQ44yzy&s=2x4gMg5K_GJPK-XUk3UfSB3hhFCziCOgqvxl7yJXTvA&ePLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotOcQ44yzy&s=50k59quZy2KmFsVBRxK4P-M7RyxDsPGieX6TiiY5or0&eand provide commented, minimal, self-contained, reproducible code.
Rui Barradas
2022-Jun-14 15:09 UTC
[R] Create a categorical variable using the deciles of data
Hello, Inline. ?s 14:49 de 14/06/2022, Ebert,Timothy Aaron escreveu:> 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."Yes, that's unfortunately true. In this particular case I've learned about pretty() because I used to use hist(., plot=FALSE) to get pretty breakpoints. And hist's documentation says it uses pretty(). 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. This thread is a good example of a "teacher" with several solutions, there are many more threads like this. Rui Barradas> > Tim > > -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Richard O'Keefe > Sent: Tuesday, June 14, 2022 9:08 AM > To: anteneh asmare <hanatezera at gmail.com> > Cc: R Project Help <r-help at r-project.org> > 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 <hanatezera at gmail.com> 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:50000)) >> # 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 at 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&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAs >> Rzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotO >> cQ44yzy&s=2x4gMg5K_GJPK-XUk3UfSB3hhFCziCOgqvxl7yJXTvA&e>> PLEASE do read the posting guide >> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or >> g_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA >> sRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuot >> OcQ44yzy&s=50k59quZy2KmFsVBRxK4P-M7RyxDsPGieX6TiiY5or0&e>> and provide commented, minimal, self-contained, reproducible code. >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotOcQ44yzy&s=2x4gMg5K_GJPK-XUk3UfSB3hhFCziCOgqvxl7yJXTvA&e> PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotOcQ44yzy&s=50k59quZy2KmFsVBRxK4P-M7RyxDsPGieX6TiiY5or0&e> and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at 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.
Richard O'Keefe
2022-Jun-16 02:36 UTC
[R] Create a categorical variable using the deciles of data
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 <tebert at ufl.edu> 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 <r-help-bounces at r-project.org> On Behalf Of Richard O'Keefe > Sent: Tuesday, June 14, 2022 9:08 AM > To: anteneh asmare <hanatezera at gmail.com> > Cc: R Project Help <r-help at r-project.org> > 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 <hanatezera at gmail.com> 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:50000)) > > # 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 at 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&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAs > > Rzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotO > > cQ44yzy&s=2x4gMg5K_GJPK-XUk3UfSB3hhFCziCOgqvxl7yJXTvA&e> > PLEASE do read the posting guide > > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or > > g_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA > > sRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuot > > OcQ44yzy&s=50k59quZy2KmFsVBRxK4P-M7RyxDsPGieX6TiiY5or0&e> > and provide commented, minimal, self-contained, reproducible code. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotOcQ44yzy&s=2x4gMg5K_GJPK-XUk3UfSB3hhFCziCOgqvxl7yJXTvA&e> PLEASE do read the posting guide > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=JNEsCwSpVwVmoMiEXA8K7ZWqg1GZK3Cx87LshtZ5gy5Y8SyDZrUSTuotOcQ44yzy&s=50k59quZy2KmFsVBRxK4P-M7RyxDsPGieX6TiiY5or0&e> and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]