-1

Is there a better way to select rownames beginning with something?
Ex.

df  
         k1   k2    p1   p2    l     perda      lP      lucroVar  
C16-C12  6.02 12.12 5.35 0.48  4.87 -1.23       3.96    79.84  
C47-C12  6.62 12.12 4.63 0.48  4.15 -1.35       3.07    75.45  
C7-C12   7.02 12.12 4.30 0.48  3.82 -1.28       2.98    74.90  
C21-C12  7.12 12.12 4.19 0.48  3.71 -1.29       2.88    74.20  
C12-C13 12.12 13.12 0.48 0.24  0.24 -0.76       0.32    24.00  
C12-C43 12.12 13.62 0.48 0.16  0.32 -1.18       0.27    21.33  

* The real data frame has 8000 rows.
The 2 following options work:

df[substr(rownames(df),1,3)=='C12',]  

or

df[grep('^C12',rownames(df)),]  

I would like

df['C12*',]  
         k1   k2    p1   p2    l     perda      lP      lucroVar 
C12-C13 12.12 13.12 0.48 0.24  0.24 -0.76       0.32    24.00  
C12-C43 12.12 13.62 0.48 0.16  0.32 -1.18       0.27    21.33    

In SQL there is "like 'C12%'".

x00
  • 93
  • 2
  • 8

2 Answers2

2

not that I would recommend doing this but..

`[.data.frame` <- function(x, i, ...) {
  base::`[.data.frame`(x, if (is.character(i)) grepl(i, rownames(x)) else i, ...)
}

letters[1]
# [1] "a"

mtcars[1, ]
#           mpg cyl disp  hp drat   wt  qsec vs am gear carb
# Mazda RX4  21   6  160 110  3.9 2.62 16.46  0  1    4    4

mtcars['M', ]
#                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4     21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
# Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
# Merc 240D     24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
# Merc 230      22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
# Merc 280      19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
# Merc 280C     17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
# Merc 450SE    16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
# Merc 450SL    17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
# Merc 450SLC   15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
# AMC Javelin   15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
# Maserati Bora 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8

And to return to normal

rm('[', '[.data.frame')
rawr
  • 20,481
  • 4
  • 44
  • 78
  • Would´n that "intervene with" [] functionality, with unpredictable results? Anyway, it is a nice and unknown way of defining functions. – x00 Mar 09 '16 at 20:22
  • @Alex very true, I have updated with a better version – rawr Mar 09 '16 at 20:30
  • why can not the function have other name, like '[~'? @rawr – x00 Mar 09 '16 at 20:34
  • @Alex an overly simple explanation (partly because I am no expert) is that when executing, say `[~`, the parse interprets this as two characters. you could certainly define a function called `[~`, but you would have to call it like `\`[~\`(df, i)`which is not what you want. r also allows you to define [infix](http://stackoverflow.com/questions/12730629/what-do-the-op-operators-in-mean-in-r-for-example-in) operators, although that is not exactly what you want, it is another weird function definition and way of calling functions in r – rawr Mar 09 '16 at 21:19
  • Tks for the class, @rawr! I like `[~(df, i)` :) – x00 Mar 09 '16 at 21:53
0

Why you dislike your approach and what do you mean by "better"? More coincise syntax or faster?

dplyr can do the same, but is actually more convoluted since I think you need to transform rownames to an explicit variable

library(dplyr)
a <- data.frame(a=(1:4), row.names=c("C12","CC12", "C1","12"))

tbl_df(cbind(a=a, b=rownames(a)))%>%
     filter(grepl("^C12", b))
Dambo
  • 3,318
  • 5
  • 30
  • 79
  • You don't need to define an extra column for the rownames. But in your example, you will run into trouble, when you don't, because one of the column has the same name as the data frame. So this: `filter(a, grepl("^C12", rownames(a)))` won't work, because in `rownames(a)`, `a` is interpreted as the column `a` in the data frame `a`. But if you call your data frame, say, `df`, this will work: `filter(df, grepl("^C12", rownames(df)))`. – Stibu Mar 09 '16 at 19:22
  • yes, a more concise code. In SQL there is "like 'C12%'". – x00 Mar 09 '16 at 19:35