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
1 answer

splitting a string and concatenating it back together

I've been trying to split a string in R and then joining it back together but none of the tricks have worked for what I need. Here's the code: I take pre-saved sentences in R sentences[1] and split it s <- str_split(sentences[1]) this is what I…
tensai
  • 51
  • 7
0
votes
0 answers

Split column into multiple columns (csv file in Python)

How can I split this column in order to have for example my column [bike] with value 0, then 1, 17 ...so on and the same for [stands]', [mechanicalBikes] ... up to a [capacity] column so that I can have numbers per date/time? I have imported it in…
Maili
  • 1
0
votes
1 answer

str_split_fixed replacing rownames

I'm using str_split_fixed() to separate taxa IDs which works fine. However, the resulting dataset has new rownames (1:nrow) but I need the original rownames. My original dataset example: > dput(tax.example) structure(list(phylodist =…
Geomicro
  • 303
  • 3
  • 13
0
votes
0 answers

Split ISO8601 format datetime time variable in R

I have aestdtc IS8601 format(like 2022-05-30T10:00) variable and want to split in date part and time. When I have tried below options , it's not working. final1 <- final %>% mutate(date = str_split_fixed(aestdtc, "T", n = 1)) when I…
Sujoy
  • 1
0
votes
1 answer

Python Extracting numeric value from {'$date': {'$numberLong': 'xxxxxxxxxxxxxx'}}

I am having trouble extracting a numeric value from a pandas DF column. I have a data frame converted from Json and it contains column values like Image "1"; Image 1 As you can imagine the thing I need is unix timestamp values from this 'dict'.…
0
votes
2 answers

How can I split up elements in a single column into multiple columns in R?

I have the following data set: df <- data.frame(identifier = c("a","b","b","c"), disease = c("heart, lung","lung, heart,,","lung, heart, heart, liver", "kidney, brain ")) which gives: identifier disease 1 …
Ahmed
  • 11
  • 3
0
votes
1 answer

How do I split cell strings if cell value contains a specific word in Python?

I am trying to write some code in python that will split the string in a cell if the cell value contains a certain word. I have made a sample dataframe to explain the challenge I'm facing. d = {'Message': [1, 2,3], 'Details': ['I WANT IT ALL',…
0
votes
5 answers

I am trying to return 3 values (State, Size, Department) but it only works if state is 1 word "Pennsylvania"

I am trying to return 3 values (State, Size, Department). You will see in the below code it works if the state is 1 word "Pennsylvania" but not if it is 2 words "New York". You can comment out New York and un-comment PA to see it work. I tried to…
0
votes
1 answer

How to split a character string into a list values so you can select one of them?

I have a list of dates, for example "1989-10-09" which are characters and I'm trying to organize them by month. By problem is that these are characters and it's difficult to extract just the month from the dates. Right now I'm trying this: a =…
Johnny
  • 59
  • 5
0
votes
1 answer

Using strsplit with lapply

I have a question with using strsplit with laaply function. I'm using titanic dataset and wants to split name by ",","." and extract "Mr", "Mrs" and so on. I tried this code and gives me error lapply(data$Name, strsplit(data$Name, split =…
0
votes
0 answers

How to use "." as pattern in stringr::str_split() function

I'm trying to split a string containing "." For example : "Who.am.I?" "Who", "am", "I?" In a similar attempt, it succeeded in splitting characters by "&" by using stringr::str_split() For example: stringr::str_split("Who&am&I?",…
0
votes
1 answer

Can't declare a vector in a recursive function C++

I'm trying to create an XML parser to load Collada files. Currently I'm defining a recursive function which can load each XML node defined as follows: XMLnode* XMLparser::loadNode(std::vector lines, unsigned int level) { …
Luke__
  • 227
  • 1
  • 9
0
votes
1 answer

Extracting part of a file path, to insert as a variable in a new column, within a loop in R

I am trying to create a merged data frame (to be written as master-adh-merge_datestamp.csv) from individual source files (.csv). Each individual source file contains data pertaining a specific month. I am using a loop function to allow me to iterate…
mda08rds
  • 59
  • 7
0
votes
1 answer

How to split each element in a string to a row in a new column?

I have this dataset: data <- data.frame(column = c("apple, banana, cherry", "apple, banana, cherry, grape", "apple, banana, cherry, grape, pear")) data column 1 apple, banana, cherry 2 apple, banana, cherry, grape 3 apple, banana, cherry,…
MetaPhilosopher
  • 131
  • 2
  • 9
0
votes
0 answers

How to split multiple choice question into multiple columns in R?

I have collected data on social media usage by using Google form. Multiple choice question were stacked in one column, which I need to split into multiple columns. But some variables were mixed with the main variable. For example a twitter column…