Dear I try to create a new subset from my dataframe. My dataframe's name is m1. "Classification Description" column has 15 different factors. The following code is used creating a subset for 1 factor. m2<-m1[m1$`Classification Description` == levels(m1$`Classification Description`)[1],] My aim is to create a subset with 4 different factors. For example, levels(m1$`Classification Description`)[1] levels(m1$`Classification Description`)[15] levels(m1$`Classification Description`)[2] levels(m1$`Classification Description`)[4] I try to following code but it didnt work m2<-m1[m1$`Classification Description` == levels(m1$`Classification Description`)[c(1,15,2,4],] How can I solve This Problem ? Example from my dataframe `Record Date` `Classification Description` `Current Month Budget Amount` <date> <fct> <dbl> 1 2019-06-30 Total On-Budget and Off-Budget Results: NA 2 2019-06-30 Off-Budget Surplus (+) or Deficit (-) 41998597035. 3 2019-06-30 Total Outlays 342428650968. 4 2019-06-30 By Other Means 51648504883. 5 2019-06-30 On-Budget Outlays 292169836521. 6 2019-06-30 Off-Budget Outlays 50258814447. 7 2019-06-30 Total Receipts 333952332514. 8 2019-06-30 On-Budget Surplus (+) or Deficit (-) -50474915489. 9 2019-06-30 Off-Budget Receipts 92257411482 10 2019-06-30 Total On-Budget and Off-Budget Financing 8476318454 -- *Sayg?lar?mla* Engin YILMAZ [[alternative HTML version deleted]]
Hello, Try %in% instead of == in: m2<-m1[m1$`Classification Description` == levels(m1$`Classification Description`)[c(1,15,2,4],] Hope this helps, Rui Barradas ?s 14:57 de 29/07/2020, Engin Y?lmaz escreveu:> Dear > > I try to create a new subset from my dataframe. > My dataframe's name is m1. > "Classification Description" column has 15 different factors. > The following code is used creating a subset for 1 factor. > m2<-m1[m1$`Classification Description` == levels(m1$`Classification > Description`)[1],] > > My aim is to create a subset with 4 different factors. For example, > levels(m1$`Classification Description`)[1] > levels(m1$`Classification Description`)[15] > levels(m1$`Classification Description`)[2] > levels(m1$`Classification Description`)[4] > > I try to following code but it didnt work > > m2<-m1[m1$`Classification Description` == levels(m1$`Classification > Description`)[c(1,15,2,4],] > > How can I solve This Problem ? > > Example from my dataframe > > `Record Date` `Classification Description` `Current Month > Budget Amount` > <date> <fct> > <dbl> > 1 2019-06-30 Total On-Budget and Off-Budget Results: > NA > 2 2019-06-30 Off-Budget Surplus (+) or Deficit (-) > 41998597035. > 3 2019-06-30 Total Outlays > 342428650968. > 4 2019-06-30 By Other Means > 51648504883. > 5 2019-06-30 On-Budget Outlays > 292169836521. > 6 2019-06-30 Off-Budget Outlays > 50258814447. > 7 2019-06-30 Total Receipts > 333952332514. > 8 2019-06-30 On-Budget Surplus (+) or Deficit (-) > -50474915489. > 9 2019-06-30 Off-Budget Receipts > 92257411482 > 10 2019-06-30 Total On-Budget and Off-Budget Financing > 8476318454 > > >-- Este e-mail foi verificado em termos de v?rus pelo software antiv?rus Avast. https://www.avast.com/antivirus
Dear Engin, On 2020-07-29 16:57 +0300, Engin Y?lmaz wrote:> Dear > > I try to create a new subset from my dataframe. > My dataframe's name is m1. > "Classification Description" column has 15 different factors. > The following code is used creating a subset for 1 factor. > m2<-m1[m1$`Classification Description` == levels(m1$`Classification > Description`)[1],] > > My aim is to create a subset with 4 different factors. For example, > levels(m1$`Classification Description`)[1] > levels(m1$`Classification Description`)[15] > levels(m1$`Classification Description`)[2] > levels(m1$`Classification Description`)[4] > > I try to following code but it didnt work > > m2<-m1[m1$`Classification Description` == levels(m1$`Classification > Description`)[c(1,15,2,4],]You're almost correct, you just need to use match instead of ==: m1[m1$`Classification Description` %in% levels(m1$`Classification Description`)[c(1, 15, 2, 4)],] Read more about it at ?match (?`%in%`). Best, Rasmus -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200729/dc629e17/attachment.sig>
I solve this as follows m2 <- subset(m1,`Classification Description`=="Borrowing from the Public" | `Classification Description`=="By Other Means" | `Classification Description`=="Total Surplus (+) or Deficit (-)") sincerely Engin YILMAZ Engin Y?lmaz <ispanyolcom at gmail.com>, 29 Tem 2020 ?ar, 16:57 tarihinde ?unu yazd?:> Dear > > I try to create a new subset from my dataframe. > My dataframe's name is m1. > "Classification Description" column has 15 different factors. > The following code is used creating a subset for 1 factor. > m2<-m1[m1$`Classification Description` == levels(m1$`Classification > Description`)[1],] > > My aim is to create a subset with 4 different factors. For example, > levels(m1$`Classification Description`)[1] > levels(m1$`Classification Description`)[15] > levels(m1$`Classification Description`)[2] > levels(m1$`Classification Description`)[4] > > I try to following code but it didnt work > > m2<-m1[m1$`Classification Description` == levels(m1$`Classification > Description`)[c(1,15,2,4],] > > How can I solve This Problem ? > > Example from my dataframe > > `Record Date` `Classification Description` `Current Month > Budget Amount` > <date> <fct> > <dbl> > 1 2019-06-30 Total On-Budget and Off-Budget Results: > NA > 2 2019-06-30 Off-Budget Surplus (+) or Deficit (-) > 41998597035. > 3 2019-06-30 Total Outlays > 342428650968. > 4 2019-06-30 By Other Means > 51648504883. > 5 2019-06-30 On-Budget Outlays > 292169836521. > 6 2019-06-30 Off-Budget Outlays > 50258814447. > 7 2019-06-30 Total Receipts > 333952332514. > 8 2019-06-30 On-Budget Surplus (+) or Deficit (-) > -50474915489. > 9 2019-06-30 Off-Budget Receipts > 92257411482 > 10 2019-06-30 Total On-Budget and Off-Budget Financing > 8476318454 > > > > -- > *Sayg?lar?mla* > Engin YILMAZ >-- *Sayg?lar?mla* Engin YILMAZ [[alternative HTML version deleted]]