mks212 mks212 - 3 months ago 13
R Question

Keep All Characters Before Backslash R

I have a list of 10 chr called data and am trying to remove everything that occurs after the first backslash but am having difficulty.

For example, here is the first string:

Nov. 3, 2016\n\t\t\t\n\t\t\t\n\t\t\t\tBO

I only want to keep Nov. 3, 2016

I am trying:

gsub('\\\\\.*', '', data)

But it is not doing the trick.

Please copy and paste the below in R to recreate the list.

data <- c("Nov. 3, 2016\n\t\t\t\n\t\t\t\n\t\t\t\tBO", "July 21, 2016\n\t\t\t\n\t\t\t\n\t\t\t\tBO",
"May 3, 2016\n\t\t\t\n\t\t\t\n\t\t\t\tBO", "Feb. 24, 2016\n\t\t\t\n\t\t\t\n\t\t\t\tBO",
"Nov. 12, 2015\n\t\t\t\n\t\t\t\n\t\t\t\tBO", "July 24, 2015\n\t\t\t\n\t\t\t\n\t\t\t\tBO",
"May 12, 2015\n\t\t\t\n\t\t\t\n\t\t\t\tBO", "Feb. 25, 2015\n\t\t\t\n\t\t\t\n\t\t\t\tBO",
"Nov. 12, 2014\n\t\t\t\n\t\t\t\n\t\t\t\tBO", "July 24, 2014\n\t\t\t\n\t\t\t\n\t\t\t\tBO")

Thanks for your help.


sub function would be enough for this case since the replacement would occur only once.,

sub("\\n[\\s\\S]*", "", x)