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

anteneh asmare h@n@tezer@ @end|ng |rom gm@||@com
Tue Jun 14 15:12:00 CEST 2022


Dear, Tim, Thanks a lot. It works perfectly.
Best,
Hana
On 6/14/22, Ebert,Timothy Aaron <tebert using ufl.edu> wrote:
> 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 <-50000
> 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 <r-help-bounces using r-project.org> On Behalf Of anteneh asmare
> Sent: Tuesday, June 14, 2022 7:29 AM
> To: r-help using 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: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-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 using 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=iJ1M9ZDgTrZDuxyw_CUg03Mb6JmtrOaSF0JqAl-1pdmgbKG3AWiI6hMbv9LVOjKN&s=eUb_8T4KZRbFW_poDuhkWwPvNKQdkI6fm0MMTsOyh-A&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=iJ1M9ZDgTrZDuxyw_CUg03Mb6JmtrOaSF0JqAl-1pdmgbKG3AWiI6hMbv9LVOjKN&s=tnk4qRX6T6SZuapvkrNEZOtHmOVlKGS-02yHEzajqS8&e=
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list