-1
Date  <- c(2000-01-01, 2000-02-02, ... )   
Abbreviation  <- c("TR_10", "SR_10", "SR_9", "FR_7", "SR_7", ...)       
Value   <- c(1.2, 1.3, 1.4, 1.8, ... )
Data <- cbind(Date,Abbreviation,Value)

How can I remove all rows, which haven't the abbreviation (initial letters) "SR_"?

Thank you!

Manuel K
  • 67
  • 7

3 Answers3

1

We can use substr

subset(Data, substr(Abbreviation, 1, 3) == "SR_")

data

Data <- data.frame(Date, Abbreviation, Value, stringsAsFactors = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You can also convert the data to a data frame and simply use filter() by the dpylr package. In addition to your code, you can have a solution like this:

Data <- data.frame(Data)

Then assign the filtered data to another dataframe.

DataFiltered <- Data %>% filter(Abbreviation %like% "SR_")

Similarly, you can use str_detect() function from stringr package for filtering. This works better, if there is a possibility that Abbreviation has entities having 'SR_' not only as the first 3 characters. You can use RegEx to specify that each entity in filtered data must have Abbreviation starting with 'SR_'.

DataFiltered <- Data %>% filter(str_detect(Abbreviation, pattern = "^SR_"))
0

Maybe you can try

Data[grep("^SR_",Data[,"Abbreviation"]),]
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81