R Subset by Factor levels

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

R Subset by Factor levels

Temel İspanyolca
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R Subset by Factor levels

Rui Barradas
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R Subset by Factor levels

Rasmus Liland-3
In reply to this post by Temel İspanyolca
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

______________________________________________
[hidden email] 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.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: R Subset by Factor levels

Temel İspanyolca
In reply to this post by Temel İspanyolca
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 <[hidden email]>, 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]]

______________________________________________
[hidden email] 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.