I have a text with comma and I need count of number characters until comma, because i want select the interval before comma.
Example:
text: "12345, Supply"
I want select just "12345".
Is it possible?
tks!
I have a text with comma and I need count of number characters until comma, because i want select the interval before comma.
Example:
text: "12345, Supply"
I want select just "12345".
Is it possible?
tks!
With sqldf
, one possibility is this
df <- data.frame(text = rep("12345, Supply", 2))
text
1 12345, Supply
2 12345, Supply
sqldf("select substr(text, 1, instr(text, ',') - 1) as text from df")
text
1 12345
2 12345
We can use sub
to match the ,
followed by one or more spaces and other characters and replace it with blank (""
)
sub(",\\s+.*", "", "12345, Supply")
#[1] "12345"
Or capture as a group and replace with the backreference
sub("^([^,]+),.*", "\\1", "12345, Supply")
#[1] "12345"