[R] Select part of character row name in a data frame
Enrico Schumann
es at enricoschumann.net
Thu Oct 19 12:12:29 CEST 2017
Quoting Francesca PANCOTTO <f.pancotto at unimore.it>:
> 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
More information about the R-help
mailing list