-1

as the title of the question says, i want to know how do i atribbute different parameters to the function

strsplit(x, split = " ")

If i apply it, i get every word in my string as a single vector, when it is separeted by space-bar. Ok. But the point is, i want also to split words that are connected with a dot (like banana.apple turning to "banana" and "apple").

I tought something like this (below) would work, but it doesnt...

strsplit(x, split = " ", "[.]")

Can anybody help me?

  • Are you trying to use multiple characters to split with? (Both space and period?) – A5C1D2H2I1M1N2O1R2T1 Dec 18 '20 at 00:17
  • Surely this has been asked before. Have you tried any searches? – IRTFM Dec 18 '20 at 00:20
  • Yeah. I mean, i have a big string (with a lot of words) and some of them have dots between; others have space-bars; others have commas... I want to use these variety of punctuation to split the words... not only the space-bar – Caio Scaravajar Dec 18 '20 at 00:22
  • I've searched for a answer for a while.... but i dont get any – Caio Scaravajar Dec 18 '20 at 00:23
  • It's interesting to see how may duplicates there are, but not all of them are exact duplicates. The cited one is the best match amount the first 4 I looked at. The downvote is for the apparent lack of any searching. You should describe efforts at searching if you want to avoid the appearance that there was none. My search was on "[r] split multiple characters". (Seemed obvious.) – IRTFM Dec 18 '20 at 00:24
  • 1
    @IRTFM, I'm not sure that's the best dupe. Perhaps [this is more appropriate](https://stackoverflow.com/q/10738729/1270695) as it seems like they're not looking at splitting into columns, which I got with ["r strsplit multiple split"](https://stackoverflow.com/search?q=%5Br%5D+strsplit+multiple+split). – A5C1D2H2I1M1N2O1R2T1 Dec 18 '20 at 00:27
  • 1
    @A5C1D2H2I1M1N2O1R2T1 The key missing bit of knowledge was how to use the "|" (pipe/OR) operator in regex patterns. The OP thought you could just add quoted characters separated by commas. So maybe https://stackoverflow.com/questions/10738729/r-strsplit-with-multiple-unordered-split-arguments is better, but I cannot substitute it for the earlier one. – IRTFM Dec 18 '20 at 00:35

1 Answers1

1

This should work if you want to split on both:

library(stringr)
x <- c("banana.apple turning.something")
str_split(x, "[\\.\\s]") 
# [[1]]
# [1] "banana"    "apple"     "turning"   "something"

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25