Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract part of string before the first semicolon

Tags:

string

r

gsub

I have a column containing values of 3 strings separated by semicolons. I need to just extract the part of the string which comes before the first semicolon.

Type <- c("SNSR_RMIN_PSX150Y_CSH;SP_12;I0.00V50HX0HY3000")

What I want is: Get the first part of the string (till the first semicolon).

Desired output : SNSR_RMIN_PSX150Y_CSH

I tried gsub without success.

like image 235
Sharath Avatar asked Apr 20 '15 15:04

Sharath


People also ask

How do you extract a certain part of a string?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

How do I extract text before a delimiter in Excel?

Extract text before or after space with formula in Excel You can quickly extract the text before space from the list only by using formula. Select a blank cell, and type this formula =LEFT(A1,(FIND(" ",A1,1)-1)) (A1 is the first cell of the list you want to extract text) , and press Enter button.

How do you get a substring before?

Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.

How do you split a string before a character?

Use the split() method to cut string before the character in Python. The split() method splits a string into a list.


2 Answers

The stringi package works very fast here:

stri_extract_first_regex(Type, "^[^;]+")
## [1] "SNSR_RMIN_PSX150Y_CSH"

I benchmarked on the 3 main approaches here:

Unit: milliseconds
      expr       min        lq      mean   median        uq      max neval
  SAPPLY() 254.88442 267.79469 294.12715 277.4518 325.91576 419.6435   100
     SUB() 182.64996 186.26583 192.99277 188.6128 197.17154 237.9886   100
 STRINGI()  89.45826  91.05954  94.11195  91.9424  94.58421 124.4689   100

enter image description here Here's the code for the Benchmarks:

library(stringi)
SAPPLY <- function() sapply(strsplit(Type, ";"), "[[", 1)
SUB <- function() sub(';.*$','', Type)
STRINGI <- function() stri_extract_first_regex(Type, "^[^;]+")

Type <- c("SNSR_RMIN_PSX150Y_CSH;SP_12;I0.00V50HX0HY3000")
Type <- rep(Type, 100000)

library(microbenchmark)
microbenchmark( 
    SAPPLY(),
    SUB(),
    STRINGI(),
times=100L)
like image 128
Tyler Rinker Avatar answered Oct 27 '22 00:10

Tyler Rinker


When performance is important you can use substr in combination with regexpr from base.

substr(Type, 1, regexpr(";", Type, fixed=TRUE)-1)
#[1] "SNSR_RMIN_PSX150Y_CSH"

Timings: (Reusing the part from @tyler-rinker)

library(stringi)
SAPPLY <- function() sapply(strsplit(Type, ";"), "[[", 1)
SUB <- function() sub(';.*$','', Type)
SUB2 <- function() sub(';.*','', Type)
SUB3 <- function() sub('([^;]*).*','\\1', Type)
STRINGI <- function() stri_extract_first_regex(Type, "^[^;]+")
STRINGI2 <- function() stri_extract_first_regex(Type, "[^;]*")
SUBSTRREG <- function() substr(Type, 1, regexpr(";", Type)-1)
SUBSTRREG2 <- function() substr(Type, 1, regexpr(";", Type, fixed=TRUE)-1)
SUBSTRREG3 <- function() substr(Type, 1, regexpr(";", Type, fixed=TRUE, useBytes = TRUE)-1)

Type <- c("SNSR_RMIN_PSX150Y_CSH;SP_12;I0.00V50HX0HY3000")
Type <- rep(Type, 100000)

library(microbenchmark)
microbenchmark(SAPPLY(), SUB(), SUB2(), SUB3(), STRINGI()
 , STRINGI2(), SUBSTRREG(), SUBSTRREG2(), SUBSTRREG3())
#Unit: milliseconds
#         expr       min        lq      mean    median        uq       max neval
#     SAPPLY() 382.23750 395.92841 412.82508 410.05236 427.58816 460.28508   100
#        SUB() 111.92120 114.28939 116.41950 115.57371 118.15573 123.92400   100
#       SUB2()  94.27831  96.50462  98.14741  97.38199  99.15260 119.51090   100
#       SUB3() 167.77139 172.51271 175.07144 173.83121 176.27710 190.97815   100
#    STRINGI()  38.27645  39.33428  39.94134  39.71842  40.50182  42.55838   100
#   STRINGI2()  38.16736  39.19250  40.14904  39.63929  40.37686  56.03174   100
#  SUBSTRREG()  45.04828  46.39867  47.13018  46.85465  47.71985  51.07955   100
# SUBSTRREG2()  10.67439  11.02963  11.29290  11.12222  11.43964  13.64643   100
# SUBSTRREG3()  10.74220  10.95139  11.39466  11.06632  11.46908  27.72654   100
like image 30
GKi Avatar answered Oct 26 '22 23:10

GKi