Hi everybody, I have a vector of character strings. Each string has the same pattern and I want to split them in pieces and get a vector made of the first pieces of each string. The problem is that strsplit returns a list. All I found is uu<- matrix(unlist(strsplit(x,";")),ncol=3,byrow=T)[,1] where x is the vector ";" is the delimiting character and I know that each string will be cut in 3 pieces. That works for my problem but I would prefer a more elegant solution. Besides, it would not work if all the string didn't have the same number of pieces. does someone have a better solution? sorry if that topic was discussed recently. There is too much traffic on the r-help list, I cannot catch up. -- Eric Elguero ------------ MIVEGEC. - UMR (CNRS/IRD/UM) 5290 Maladies Infectieuses et Vecteurs, G?n?tique, Evolution et Contr?le Institut de Recherche pour le D?veloppement (IRD) 911, Avenue Agropolis BP 64501 34394 Montpellier Cedex 5, France
Hi,
I'm not sure about the more generalized solution, but how about this for a
start.
x <- c("a;b;c", "d;e", "foo;g;h;i")
x
#[1] "a;b;c" "d;e" "foo;g;h;i"
sapply(strsplit(x, ";",fixed = TRUE), '[',1)
#[1] "a" "d" "foo"
If you want elegance then I suggest you take a look at the stringr package.
https://cran.r-project.org/web/packages/stringr/index.html
Cheers,
Ben
> On Jul 21, 2016, at 7:54 AM, Eric Elguero <Eric.Elguero at ird.fr>
wrote:
>
> Hi everybody,
>
> I have a vector of character strings.
> Each string has the same pattern and I want
> to split them in pieces and get a vector made
> of the first pieces of each string.
>
> The problem is that strsplit returns a list.
>
> All I found is
>
> uu<- matrix(unlist(strsplit(x,";")),ncol=3,byrow=T)[,1]
>
> where x is the vector ";" is the delimiting character
> and I know that each string will be cut in 3 pieces.
>
> That works for my problem but I would prefer a
> more elegant solution. Besides, it would not
> work if all the string didn't have the same
> number of pieces.
>
> does someone have a better solution?
>
> sorry if that topic was discussed recently.
> There is too much traffic on the r-help list,
> I cannot catch up.
>
> --
> Eric Elguero
> ------------
> MIVEGEC. - UMR (CNRS/IRD/UM) 5290
> Maladies Infectieuses et Vecteurs, G?n?tique, Evolution et Contr?le
> Institut de Recherche pour le D?veloppement (IRD)
> 911, Avenue Agropolis
> BP 64501
> 34394 Montpellier Cedex 5, France
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
Report Gulf of Maine jellyfish sightings to jellyfish at bigelow.org or tweet
them to #MaineJellies -- include date, time, and location, as well as any
descriptive information such as size or type. Learn more at
https://www.bigelow.org/research/srs/nick-record/nick-record-laboratory/mainejellies/
Dear Eric I think you are looking for sub or gsub Without an example set of input and output I am not quite sure but you would need to define an expression which matches your separator (;) followed by any characters up to the end of line. If you have trouble with that then someone here will no doubt write the pattern for you but learning about regular expressions is well worthwhile On 21/07/2016 12:54, Eric Elguero wrote:> Hi everybody, > > I have a vector of character strings. > Each string has the same pattern and I want > to split them in pieces and get a vector made > of the first pieces of each string. > > The problem is that strsplit returns a list. > > All I found is > > uu<- matrix(unlist(strsplit(x,";")),ncol=3,byrow=T)[,1] > > where x is the vector ";" is the delimiting character > and I know that each string will be cut in 3 pieces. > > That works for my problem but I would prefer a > more elegant solution. Besides, it would not > work if all the string didn't have the same > number of pieces. > > does someone have a better solution? > > sorry if that topic was discussed recently. > There is too much traffic on the r-help list, > I cannot catch up. >-- Michael http://www.dewey.myzen.co.uk/home.html