Select part of character row name in a data frame

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

Select part of character row name in a data frame

Francesca PANCOTTO-4
Dear R contributors,

I have a problem in selecting in an efficient way, rows of a data frame according to a condition,
which is a part of a row name of the table.

The data frame is made of 64 rows and 2 columns, but the row names are very long but I need to select them according to a small part of it and perform calculations on the subsets.

This is the example:
                                                                                        X Y
"Unique to strat          "                                                    0.0482        28.39
"Unique to crt.dummy        "                                                  0.0441        25.92
"Unique to gender                   "                                          0.0159         9.36
"Unique to age                       "                                         0.0839        49.37
"Unique to gg_right1              "                                            0.0019         1.10
"Unique to strat:crt.dummy                 "                                   0.0689        40.54
"Common to strat, and crt.dummy         "                                     -0.0392       -23.09
"Common to strat, and gender         "                                        -0.0031        -1.84
"Common to crt.dummy, and gender     "                                         0.0038         2.21
"Common to strat, and age                 "                                    0.0072         4.21

X and Y are the two columns of variables, while “Unique to strat”, are the row names. I am interested to select for example those rows
whose name contains “strat” only. It would be very easy if these names were simple, but they are not and involve also spaces.
I tried with select matches from dplyr but works for column names but I did not find how to use it on row names, which are of course character values.

Thanks for any help you can provide.

----------------------------------
Francesca Pancotto, PhD

______________________________________________
[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: Select part of character row name in a data frame

Enrico Schumann-2

Quoting Francesca PANCOTTO <[hidden email]>:

> Dear R contributors,
>
> I have a problem in selecting in an efficient way, rows of a data  
> frame according to a condition,
> which is a part of a row name of the table.
>
> The data frame is made of 64 rows and 2 columns, but the row names  
> are very long but I need to select them according to a small part of  
> it and perform calculations on the subsets.
>
> This is the example:
> X Y
> "Unique to strat          "                                          
>           0.0482        28.39
> "Unique to crt.dummy        "                                        
>           0.0441        25.92
> "Unique to gender                   "                                
>           0.0159         9.36
> "Unique to age                       "                                
>           0.0839        49.37
> "Unique to gg_right1              "                                  
>           0.0019         1.10
> "Unique to strat:crt.dummy                 "                          
>           0.0689        40.54
> "Common to strat, and crt.dummy         "                            
>          -0.0392       -23.09
> "Common to strat, and gender         "                                
>          -0.0031        -1.84
> "Common to crt.dummy, and gender     "                                
>           0.0038         2.21
> "Common to strat, and age                 "                          
>           0.0072         4.21
>
> X and Y are the two columns of variables, while “Unique to strat”,  
> are the row names. I am interested to select for example those rows
> whose name contains “strat” only. It would be very easy if these  
> names were simple, but they are not and involve also spaces.
> I tried with select matches from dplyr but works for column names  
> but I did not find how to use it on row names, which are of course  
> character values.
>
> Thanks for any help you can provide.
>
> ----------------------------------
> Francesca Pancotto, PhD
>

Use ?grep or ?grepl:

     df[grep("strat", row.names(df)), ]

(in which 'df' is your data frame)


--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

______________________________________________
[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: Select part of character row name in a data frame

Francesca PANCOTTO-4
Thanks a lot, so simple so efficient!

I will study more the grep command I did not know.

Thanks!


Francesca Pancotto

> Il giorno 19 ott 2017, alle ore 12:12, Enrico Schumann <[hidden email]> ha scritto:
>
>  df[grep("strat", row.names(df)), ]


        [[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: Select part of character row name in a data frame

Jeff Newmiller
(Re-)read the discussion of indexing (both `[` and `[[`) and be sure to get clear on the difference between matrices and data frames in the Introduction to R document that comes with R. There are many ways to create numeric vectors, character vectors, and logical vectors that can then be used as indexes, including the straightforward way:

df[ c(
"Unique to strat          ",
"Unique to strat:crt.dummy                 ",
"Common to strat, and crt.dummy         ",
"Common to strat, and gender         ",
"Common to strat, and age                 ") ,]
--
Sent from my phone. Please excuse my brevity.

On October 19, 2017 3:14:53 AM PDT, Francesca PANCOTTO <[hidden email]> wrote:

>Thanks a lot, so simple so efficient!
>
>I will study more the grep command I did not know.
>
>Thanks!
>
>
>Francesca Pancotto
>
>> Il giorno 19 ott 2017, alle ore 12:12, Enrico Schumann
><[hidden email]> ha scritto:
>>
>>  df[grep("strat", row.names(df)), ]
>
>
> [[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.

______________________________________________
[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: Select part of character row name in a data frame

Francesca PANCOTTO-4
I did not need to select the whole character sentence, otherwise I would know how to do it.. from basic introduction to R as you suggest.
Grep works perfectly.

f.

----------------------------------
Francesca Pancotto, PhD

> Il giorno 19 ott 2017, alle ore 18:01, Jeff Newmiller <[hidden email]> ha scritto:
>
> (Re-)read the discussion of indexing (both `[` and `[[`) and be sure to get clear on the difference between matrices and data frames in the Introduction to R document that comes with R. There are many ways to create numeric vectors, character vectors, and logical vectors that can then be used as indexes, including the straightforward way:
>
> df[ c(
> "Unique to strat          ",
> "Unique to strat:crt.dummy                 ",
> "Common to strat, and crt.dummy         ",
> "Common to strat, and gender         ",
> "Common to strat, and age                 ") ,]
> --
> Sent from my phone. Please excuse my brevity.
>
> On October 19, 2017 3:14:53 AM PDT, Francesca PANCOTTO <[hidden email]> wrote:
>> Thanks a lot, so simple so efficient!
>>
>> I will study more the grep command I did not know.
>>
>> Thanks!
>>
>>
>> Francesca Pancotto
>>
>>> Il giorno 19 ott 2017, alle ore 12:12, Enrico Schumann
>> <[hidden email]> ha scritto:
>>>
>>> df[grep("strat", row.names(df)), ]
>>
>>
>> [[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.

______________________________________________
[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.