Subset by line number in magrittr chain

I am experimenting with magrittr chaining using grep / gsub etc.

It works well

top_url <- "http://www.england.nhs.uk/statistics/statistical-work-areas/ae-waiting-times-and-activity/"

readLines(top_url) %>% grep("SitReps", .)

      

The next step is to return the subset using line numbers. I tried this but it doesn't work.

readLines(top_url) %>% .[grep("SitReps", .)]

      

Can this be done?

+3


source to share


2 answers


Another option would be to just use value = TRUE

internally grep

(which will save you one extra operator)

readLines(top_url) %>% grep("SitReps", ., value = TRUE)

      



Or just change your own code and use [

like this

readLines(top_url) %>% `[`(grep("SitReps", .))

      

+3


source


Besides the option in David's comment, you can also:

readLines(top_url) %>% extract(grep("SitReps", .))

      



But I would prefer David's approach.

Note that you are only multiplying a character vector here, which has no line numbers.

+3


source







All Articles