1

I am trying to extract the first time stamp from the following character:

"WHENSEPTEMBER 14, 2015 @ 11:56 AM - 12:00 PM EDT"

I have a whole list of them and they are vectors, as required.

> is.vector(data$description)
[1] TRUE
> is.vector(data$info)
[1] TRUE

>str_extract(data$info, "\\@ [0-9]+:[0-9]{2}")
Error in stri_extract_first_regex(string, pattern, opts_regex = attr(pattern,  : 
  argument `str` should be a character vector (or an object coercible to)

I realize that my regex is not entirely correct yet to extract only the first time. But at this point it won't extract anything. I am not sure what I am doing wrong here. Any help is appreciated.

user3859248
  • 97
  • 2
  • 11
  • Can you post the output dput(data), because I get "@ 11:56" when I try this. – blep Sep 12 '15 at 03:54
  • 1
    Listen to the feedback and provide dput(data) or str(data). If you read carefully about is.vector I think you will realize that it is misleading you. – joran Sep 12 '15 at 04:34
  • `str` is a good friend who reveals much. Have you `str`-ed `data`? – Tyler Rinker Sep 12 '15 at 04:55
  • Could you post the `str()` of your data, as already requested twice? –  Sep 12 '15 at 05:56
  • Without a reproducible example this is not solvable. I voted to close. – Tyler Rinker Sep 12 '15 at 12:55
  • Thank you everyone for your feedback. Sorry I was not able to reply sooner, but your instructions guided me in the right direction and I was able to resolve my problem! You were right is.vector is misleading. Thank you for your patience – user3859248 Sep 12 '15 at 15:00

2 Answers2

2

It is not exactly clear whether we need only the 'HH:MM' part or need the AM/PM as well. If this is only to extract the 'HH:MM' after the @ followed by a space, we can use regex lookarounds ((?<=\\@ )) to match the numbers followed by : followed by two digits. Also, specify the regex or fixed.

library(stringi)
stri_extract(v1,  regex="(?<=\\@ )[0-9]+:[0-9]{2}")
#[1] "11:56"

Or using stringr

library(stringr)
str_extract(v1, "(?<=\\@ )[0-9]+:[0-9]{2}")
#[1] "11:56"

data

v1 <- "WHENSEPTEMBER 14, 2015 @ 11:56 AM - 12:00 PM EDT"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • you are using the stringi package whereas I am using stringr package. Nevertheless, this does not work when I use your regex. I am not worried about the regex, I am worried about the fact that the function isn't working at all and returning me the error shown in the question – user3859248 Sep 12 '15 at 04:27
  • @user3859248 Sorry, i got mistaken. But, `stringr` is a wrapper for `string`i`. BTW, if you are using `stringr`, you don't need to specify the `regex=`. It works for me. – akrun Sep 12 '15 at 07:09
  • @user3859248 BTW, I am using `stringr_1.0.0` on `R 3.2.2.` – akrun Sep 12 '15 at 07:32
  • 1
    Thank you, yes so I figured the problem would persist anyway, it seems like I did not really have a vector, as I thought I did. Thank you for your help, your regex extracted exactly what I wanted and it works now, thank you! – user3859248 Sep 12 '15 at 15:01
  • @user3859248 Thank you for the feedback. – akrun Sep 12 '15 at 15:04
-1

Something seems to be wrong with your data vector. The functions works as it should on the string you provided:

x <- "WHENSEPTEMBER 14, 2015 @ 11:56 AM - 12:00 PM EDT"
x
[1] "WHENSEPTEMBER 14, 2015 @ 11:56 AM - 12:00 PM EDT"
str_extract(x, "\\@ [0-9]+:[0-9]{2}")
[1] "@ 11:56"
vaettchen
  • 7,299
  • 22
  • 41