1

So I am attempting to use grep to find pattern and replace values within my single column data frame. I basically want grep that says "delete everything after the comma until the end of the string". I wrote the expression, and it works on my dummy vector:

> library(stringr)
> pretendvector <- c("Hi","Hi,there","Hi there, how are you")
>str_replace(pretendvector, regex(',.*$'),'')
[1] "Hi"       "Hi"       "Hi there"

However, when apply the same expression to my vector (since its for stringr I vectorized the column of the dataframe), it returns every value in the column, and does not apply the expression. Does anyone have any idea why this might be?

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
ALW94
  • 23
  • 2
  • It is not clear what you meant by `it return every value in the column` did you assign the output to some object? i.e. `newvector <- str_replace(pretendvector, regex(',.*$'),'')` – akrun May 26 '16 at 02:44

1 Answers1

0

I guess the OP didn't assign the output from str_replace to a new object or update the original vector. In that case,

newvector <- str_replace(pretendvector, regex(',.*$'),'') 

We can also do this using sub from base R

newvector <- sub(",.*", "", pretendvector)
akrun
  • 874,273
  • 37
  • 540
  • 662