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
0
votes
3 answers

Split text by headings as delimiters and save as dataframe columns in R

I have data frame of drugs (df) and their associated information in a text column with a number of headings (two of which are provided as examples). I need to split the text and have the according text in separate columns (as provided in the…
ayeh
  • 48
  • 10
0
votes
1 answer

can you capture multiple substrings in a Panda dataframe column using .str.split() in one step

I have a database with a column in a Pandas dataframe that contains a string that has two substrings (a string & a number) I extract. I do it with str.split(), but not in a very pythonic manner because split, for me at least, returns the first item…
wiseass
  • 543
  • 2
  • 5
  • 11
0
votes
4 answers

How do I sum the numbers of a string vertically?

I'm trying to add all the numbers in a string in the Python language. For example, s="""11 9 5 6 6 8 4 6 4""" If we considered this string in 3 lines, and each line separated with "Enter" and some space between them, how could we this…
0
votes
2 answers

Split string and concatenate removing whole word in R

I am trying to remove the words "Arts and Humanities" and "Social Sciences" from a string containing concatenated by "/" different disciplines of knowledge as follows: string = "Arts and Humanities Other Topics/Social Sciences Other Topics/Arts and…
0
votes
1 answer

Trouble turning a text file with a character string into a column with one character per row in R

I am relatively new to R and am attempting to turn a text file with a long character string into a single column of a data table with one character per row. I have tried reading in the text file using read_file from the readr package and then making…
MBrad
  • 5
  • 1
0
votes
1 answer

R - Identify words in a comma-seperated list for a specific column in a dataframe

I have a specific column in a dataframe, where each cell of that column has a list of comma-seperated words without spaces. I am wanting to pick out the presence of (either of) two specific words in each cell, and when that presence is detected then…
Prakash_S
  • 77
  • 5
0
votes
1 answer

R: splitting columns in two but keep the rest

Hi I have this table here: variable level vaccinated = 0 (n=16455) vaccinated = 1 (n=1297 Total (n=17752) p-value Sex M 8,586 (52.2) 714(55.1) 9,3023 (12,1) 1,22 F 1,2323(12,1) 1,2323(12,1) 9,3023…
Mari
  • 1
  • 2
0
votes
3 answers

Is strsplit the fastest way to separate a string in R

I have a string, "1500|3|10000|5" and I wish to have a numeric vector like so: [1] 1500 3 10000 5 strsplit is much faster than str_extract_all. Is strsplit the fastest way to do this? library("tidyverse") library("microbenchmark") x <-…
ixodid
  • 2,180
  • 1
  • 19
  • 46
0
votes
1 answer

Adding a column to a df based on comparison with a list through strsplit() in R

I've been working on something for a while now and still haven't figured out how to get it to work in my preferred way. Hoping someone can help me: I have a dataframe containing lots of data (5000+ obs) about city budgets, therefore, one of the…
Demi
  • 33
  • 1
  • 5
0
votes
1 answer

How can I define if statements depending on the type of the separation using strsplit in R?

I am trying to create a Shiny app using textInput. The idea is that the user could be able to enter a list of words in order to use it. I have found this post which allows me to split the words into a vector. However, it depends how the user decides…
emr2
  • 1,436
  • 7
  • 23
0
votes
1 answer

R: Splitting a string into multiple columns

I am working with the following column string in a dataframe s <- "[08/26/2021 06:58:12 260] - MacId: 40_E3_D6_CA_56_5C RSSI: -92" I would like to split that somehow into the following columns df$Datetime = 08/26/2021 06:58:12 260 df$MacId =…
Glenn
  • 17
  • 7
0
votes
4 answers

How to split a column if a delimiter exists, else retain the same value using R

My sample data frame looks like below. I need to split the column into 2 based on a period(.) delimiter if exists, else the same value needs to be retained. df_col1 server2.rty.com datasserver server1.rty.network datasource I…
MSM
  • 69
  • 7
0
votes
3 answers

Add in missing decimal (001=0.01 and 01=0.1)

I have a vector of strings a<-data.table(value=("001","01")) that really should be decimals (0.01 and 0.1) and would liked to split the strings at the first 0 and add in a decimal before it. I can split the string and get a list A<-strsplit(a$value,…
HCAI
  • 2,213
  • 8
  • 33
  • 65
0
votes
1 answer

Using str_split() on a specific string containing a "." in R

I am trying to split a string into multiple bits when a specific set of characters is found. In this example "abc." Here is my code test <- "aa\abc.def/def\abd.abd...abc.def" result <- str_split(test,"abc\\.") So here my expected output is "aa\"…
LBes
  • 3,366
  • 1
  • 32
  • 66
0
votes
1 answer

How to split a Info column in my Dataframe to 6 separate columns?

The df dataframe consists of one column called Info which has some words separated by ' '. I want to split these and store them in separate columns. I created the columns and tried the str.split(' ',1).toList() but it shows a ValueError cols =…
nsakash
  • 29
  • 5