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 and replacing a character variable in a dataframe in R

I have a dataframe with multiple character variables of different lengths, and I would like to convert each variable to a list, with each element containing each word, split by spaces. Say my data looks like this: char <- c("This is a string of…
0
votes
2 answers

String rearrangment in R

I have a long list of names of city and its province name. This is partial list of my data data <- c('Ranchi_Capital_State_Jharkhand', 'Bokaro_State_Jharkhand', 'Tata Nagar_State_Jharkhand', 'Ramgarh_State_Jharkhand', 'Pune_State_Maharashtra',…
Rajan
  • 453
  • 4
  • 22
0
votes
1 answer

Splitting column of a data.frame in R using gsub

I have a data.frame called rbp that contains a single column like following: >rbp V1 dd_smadV1_39992_0_1 Protein: AGBT(Dm) Sequence Position 234 290 567 126 Protein: ATF1(Dm) Sequence Position 534 …
Carol
  • 367
  • 2
  • 3
  • 18
0
votes
0 answers

In R a error occurs in write.csv after strsplit

I have a dataframe which once I split the terms in col (X) the DF becomes un-writable. I am prompted with this error Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, : unimplemented type 'list' in 'EncodeElement' My…
George
  • 903
  • 8
  • 22
0
votes
2 answers

separating into columns in r - delimiter changes

I want to split values contained in a single column into new columns. I have some data that looks like this in a file: > df V1 1 00006303657102064942660780914135165036 12867 15476 15473 15474 15397 14050 2 00006319625527159782351492300309533775…
vagabond
  • 3,526
  • 5
  • 43
  • 76
0
votes
2 answers

Merge columns in data.frame after removal of duplicate strings

I have a data.framedata of character vectors as follows. x <- c("kal, Kon, Jor, Kara", "Bruce, Helena, Martha, Terry", "connor, oliver, Roy", "Alan, Guy, Simon, Kyle") y <- c("Mon, Cir, John, Jor", "Damian, Terry, Jason", "Mia, Roy", "John,…
Crops
  • 5,024
  • 5
  • 38
  • 65
0
votes
1 answer

R - getting back single element from strsplit

I have a character vector separated by "_". I want to loop through each string split it and then use the individual values to grab specific rows/cols of a csv file to do some calculations. How can I get the individual elements? tst <-…
user3646105
  • 2,459
  • 4
  • 14
  • 18
0
votes
1 answer

R Splitting strings with pattern of irregular lengths

I have a data.frame column with +3000 strings, which I would like to have separated, but they are irregular although with a pattern. Here are some examples, and what I would like them converted…
Marcus
  • 437
  • 4
  • 9
0
votes
1 answer

Using strsplit with multiple separators

A character string of interest can either be 'there are five apples' or 'there are five APPLES' strsplit(string, c('apples', 'APPLES')) So I want to split by either apples or APPLES because I don't know if the string is going to have lower case…
Adrian
  • 9,229
  • 24
  • 74
  • 132
0
votes
1 answer

R: searching within split character strings with apply

Within a large data frame, I have a column containing character strings e.g. "1&27&32" representing a combination of codes. I'd like to split each element in the column, search for a particular code (e.g. "1"), and return the row number if that…
Kim Phan
  • 17
  • 2
0
votes
2 answers

trasforming text into matrix to become .csv in R

I have the following text: Anada - Asociación de nada Address: calle 13 13 Medellin Colombia Other address: Phone.: 13-13-136131 13-13-13-1313 E-mail: anada@13.co Web page: Category: 3. Private sector Notes: Atodo - Asociación de todo Address: calle…
xav
  • 111
  • 6
0
votes
1 answer

Strange behavior of strsplit() in R?

I would like to split the string x = "a,b," (comma at the last place) into the vector c("a","b","") using strsplit(). The result is: >strsplit(x,',') [[1]] [1] "a" "b" I would like the have the third component (empty string or NULL). The function…
0
votes
3 answers

Speed up a loop in r, using character strings simplification

I have a data frame sp which contains several species names but as they come from different databases, they are written in different ways. For example, one specie can be called Urtica dioica and Urtica dioica L.. To correct this, I use the…
user3443183
  • 115
  • 6
0
votes
2 answers

How can I split a string and add them to vector?

I'd like to split a character vector so that additional members are added to the length of the vector. > va <- c("a", "b", "c;d;e") [1] "a" "b" "c;d;e" > vb <- strsplit(va, ";") [[1]] [1] "a" [[2]] [1] "b" [[3]] [1] "c" "d" "e" Can can I…
Jay
  • 741
  • 10
  • 26
0
votes
2 answers

Using strsplit when required split character vector is not consistent for all observations in variable (R)

I have data that looks like the following: duration obs another 1 1.801760 ID: 10 DAY: 6/10/13 S orange 2 1.868500 ID: 10 DAY: 6/10/13 S green 3 0.233562 ID: 10 DAY: 6/10/13 S yellow 4 5.538760 …
jalapic
  • 13,792
  • 8
  • 57
  • 87