Questions tagged [strsplit]

strsplit is a function in R and MATLAB which splits the elements of a character vector around a given delimiter.

strsplit is a function in R (documentation) and MATLAB (documentation), which splits the elements of a character vector into substrings:

# R:  
strsplit(x, split, fixed=FALSE)
% MATLAB
strsplit(x, split);

Splits a character string or vector of character strings using a regular expression or a literal (fixed) string. The strsplit function outputs a list (R) or cell array (MATLAB), where each list item corresponds to an element of x that has been split.

  • x a character string or vector of character strings to split.
  • split the character string to split x.
    In R, if split is an empty string (""), then x is split between every character.
  • [R only:] fixed if the split argument should be treated as fixed (i.e. literally). By default, the setting is FALSE, which means that split is treated like a regular expression.
702 questions
-1
votes
1 answer

split character combine and reshape dataframe in R

I am trying to built a patent network. I have a sample dataframe (aa) that contains an ID variable (origin) and string character (Target). I want to split the string character into separate groups and then add it back to the dataframe in long…
-1
votes
1 answer

strsplit function in R for data.table

I have a table where one of my Columns (mydata$Gene) has some ID's which are in the format: ENSG00000000419.8 ENSG00000000460.12 I wish to understand how to use the strsplit function to remove the .xx part so I want all my outputs to come out as…
-1
votes
2 answers

Split salary range

I am trying to split the first column range into two separate columns as so I have tried the following code: testDF$IncomeLowerRange <- strsplit(gsub("[^-]+-", "", testDF$IncomeRange), ";")[1] testDF$IncomeUpperRange <- strsplit(gsub("[^-]+-", "",…
CRich
  • 118
  • 1
  • 1
  • 7
-1
votes
1 answer

Interpretting error messages in R, splitting one column into two and creating a new dataframe

I've scraped two tables into R, trimmed them, and now I seek to create new tables by splitting one of the columns of the originals into two. To do this, I've written the following code: page.201702050atl =…
DataProphets
  • 156
  • 3
  • 17
-1
votes
2 answers

Select elements from a character vector with strsplit

I have a character vector which looks like this: chars <- c("Classics football boots", "Classics football shoes","football shoes", "new footbal shoes") In this chars object i want to select only elements football shoes & football boots. With…
Sander Van der Zeeuw
  • 1,092
  • 1
  • 13
  • 35
-1
votes
2 answers

How to split up a Python list in R

I have a list created in Python embedded into a cell of a csv. I am trying to coerce the elements into a datatable in R, but I am stuck on one particular vector which contains text. The reason is that while strsplit() works fine with the numeric…
ModalBro
  • 544
  • 5
  • 25
-1
votes
2 answers

Split a column in R

In my data frame I have some semi-structured data in a column. df col1 a|b|c a b1|b|c a & b2|b|c 3 from this dataframe$col1 I want to extract only the first word before the "|". I tried using this df$col2 <-…
amith
  • 399
  • 1
  • 2
  • 11
-1
votes
2 answers

Each character own element

I have a character vector x <- "nkiLVkqspmLVAydnaVNLSCkys" I want to split it into a vector with 25 elements, so that: x[1] # [1] "n" x[2] # [1] "k" # and so on ... The only thing I can think of is to do a regex replace any alpha character with…
user2909302
  • 103
  • 8
-1
votes
1 answer

R: how to separate external text data by section

I have a dataset from a text file that has simply 2 columns but multiple section breaks in data, that I want to place into separate arrays, where the name of the array is the text in the 2nd column adjacent to "Ran:". Below is an sample…
crazian
  • 649
  • 4
  • 12
  • 24
-2
votes
2 answers

Splitting text and numbers and adding a seperator

I have a string of the form: Abu Dhabi1.90Morrisville Samp Army1.90 Deccan Gladiators1.40The Chennai Braves2.87 Bangla Tigers1.90Delhi Bulls1.90 New Zealand1.68India2.15 Australia1.09Draw14.00West Indies13.00 Sri Lanka1.51Afghanistan2.50 Tas…
mrtapan19
  • 1
  • 1
-2
votes
1 answer

Is there a way to divide a single variable in R into two variables?

I am looking to divide this into two variables. Ideally, I would have one variable with just the items, and another with just the numbers. Of course, deleting the intermittent periods is the biggest challenge I face. Anyone have any tips? Thanks in…
Abc
  • 1
  • 1
-2
votes
1 answer

Why is this subprocess.check_output line crashing my script?

I have a script that works fine when in a .pyw, but when converted to an .exe, (EDIT: actually, when I use pyinstaller with the arguments -w or --windowed or --noconsole it doesn't work, but without them it works) I've found that this one single…
jangles
  • 303
  • 1
  • 6
  • 22
-2
votes
2 answers

How to put character or separator before a string in R

I have a string like : "Father’s Name : ABC NaskarDate of Birth : 18-01-1979Permanent Address: This is the address field for the personContact Numbers : 98413***28Passport Number:PAN Number: AEFXXXXXXXLanguages Known: Tamil, English" My desired…
A.Naskar
  • 49
  • 7
-2
votes
1 answer

How to manipulate a split data frame to add a new vector based on grep in R

I have one column in a CSV of salary data with the following types of data: Nothing in cell "£2.5-£3.5 an hour" "£4.8 an hour" "£40,000-£60,000 a year" £60,000 a year My objective is to: Split this df$salary column into as many columns as…
Dhruv Ghulati
  • 2,976
  • 3
  • 35
  • 51
-2
votes
3 answers

php cut string from end of string

I have a string like any of the following: $str = 'A001'; $str = 'B001'; $str = 'AB001'; $str = 'B0015'; .... $str = '001A'; I want to keep only 3 characters from the end of each string. My code is like this: $code = str_split($str); $code =…
koe
  • 736
  • 1
  • 12
  • 33
1 2 3
46
47