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
4 answers

Vectorizing a function that uses strsplit

I am trying to make a function that converts time (in character form) to decimal format such that 1 corresponds to 1 am and 23 corresponds to 11 pm and 24 means the end of the day. Here are the two function that does this. Here one function…
Stat-R
  • 5,040
  • 8
  • 42
  • 68
0
votes
1 answer

How to read and split words of a text file and then save in a single variable (MATLAB)

Given below is my text inside a text file: annotations/01/1515.eng Yacare Ibera an alligator in the water; Corrientes, Argentina August…
user3416063
  • 155
  • 1
  • 11
0
votes
0 answers

strsplit error (easy) [R]

For life of me, I don't know what is wrong here and thought there might be something I am doing wrong: As with all my other stuff I've asked, this related to genetics I'm trying to split genetic variables with a string such as A/A, with the…
user2726449
  • 607
  • 4
  • 11
  • 23
0
votes
2 answers

How to Split a String into two different elements in R?

I have a string like c <- "Gary INMetro Chicago IL Metro" I am doing d <- strsplit(c,"Metro") to get > d[1] [[1]] [1] "Gary IN" " Chicago IL " But I want two different elements and want to write to the csv file as City,State Gary,IN …
user3188390
  • 603
  • 2
  • 11
  • 19
0
votes
2 answers

Read names of variable complexity without delimiters, e.g. baseball players

I have an external data file like below, with no delimiters: PLAYER TEAM STUFF1 STUFF2 Jim Smith NYY 100 200 Jerry Johnson Jr. PHI 100 200 Andrew C. James STL 200 200 A. J. Williams CWS 100 200 Felix Rodriguez BAL 100 100 How can…
Mark Miller
  • 12,483
  • 23
  • 78
  • 132
0
votes
1 answer

R, readLines, strsplit and grep

I am trying to read a random text file one line at a time. Then split the line into "words" and perform some regex on each word, like finding all word that start with "w". After the following like code snippet I get: while (length(oneLine <-…
user2970161
  • 11
  • 1
  • 4
0
votes
1 answer

R: how to avoid strsplit hiccuping on empty vectors when splitting text

Have a list of text-sections which are required to be split into sentences by: > textList <- list(sections=sections[(length(sections)-2):length(sections)]) > textList$sentences <- sapply(textList$sections, function(x) strsplit(as.character(x),…
alex
  • 1,103
  • 1
  • 14
  • 25
0
votes
4 answers

hangman "word" in php

I am trying to do something similar to hangman where when you guess a letter, it replaces an underscore with what the letter is. I have come up with a way, but it seems very inefficient and I am wondering if there is a better way. Here is what I…
scott
  • 41
  • 2
0
votes
2 answers

how to split column by | into multiple columns

In R: I have a dataframe of many rows but only one column. Each row has a long string of characters, periodically punctuated with a | mark. I want to split the characters every time there is a | mark, so that there are many…
0
votes
2 answers

splitting comma separated mixed text and numeric string with strsplit in R

I have many strings of the form name1, name2 and name3, 0, 1, 2 or name1, name2, name3 and name4, 0, 1, 2 and would like to split the vector into 4 elements where the first one would be the whole text string of names. The problem is that strsplit…
nopeva
  • 1,583
  • 5
  • 22
  • 38
0
votes
1 answer

Split characters by colon in several columns in R

I have some data which I have already split them by colons. I want to split all the contents of each column by colon. Here is an example of my data: V8 V9 V10 1…
0
votes
1 answer

Applying strsplit function for multiple files

could somebody spot what I am doing wrong in the step 4 of my codes please (strsplit)? I have 3 files in the directory with names '101_E45_N66.csv','102_E46_N76.csv', '103_E47_N86.csv' I am reading them in a list and trying to split the names and…
Achak
  • 1,286
  • 2
  • 18
  • 36
-1
votes
2 answers

names attribute [3] must be the same length as the vector [2]

Pls how can i rename each columns after str_split() operations in data frames Here i created example data set library(stringr) #create data frame samples <- paste0("sample", 1:5) tags <- paste("tag", LETTERS[1:5],sep="_") df1 <-…
sahuno
  • 321
  • 1
  • 8
-1
votes
1 answer

i made custom strsplit function but output is weird

#include using namespace std; int ft_strlen(char *str, char ascii[]) { int i = 0; while (!ascii[(short)str[i]] && str[i]) ++i; return (i); } int word_count(char *s, char *ascii) { int i = 0, cnt = 0; while…
neco_cat
  • 1
  • 1
-1
votes
1 answer

How do I extract numbers from list of strings

I have a Python list: List1 = ["mohit23", "jimmy24"] I want output as: [23, 24]