Delete text after second space

I have a matrix like this (each row is a row):

m <- matrix(c("Agarista revoluta (Spreng.) Hook. f. ex Nied.", 
              "Amaioua intermedia Mart.", 
              "Baccharis reticularia DC."),, 1)

I would like to remove the text after the second space and return:

Agarista revoluta
Amaioua intermedia
Baccharis reticularia

I tried several combinations with gsub, but I failed.

Can anyone help me with this?

+7
source share
4 answers

you can use

x <- c("Agarista revoluta (Spreng.) Hook. f. ex Nied.", "Amaioua intermedia Mart.", "Baccharis reticularia DC.")
sub("^(\\S*\\s+\\S+).*", "\\1", x)
## => [1] "Agarista revoluta"     "Amaioua intermedia"    "Baccharis reticularia"

Watch the regex demo and the online R demo.

Template Details :

  • ^ - beginning of line
  • (\\S*\\s+\\s+) - group 1 captures 0+ non-space characters, then 1+ spaces and then 1+ non-spaces
  • .* - any characters 0+, as much as possible (to the end of the line).

, , ,

sub("^\\s*(\\S+\\s+\\S+).*", "\\1", x)

R

+6

sub('^(\\w+\\s+\\w+).*', '\\1', x)
#[1] "Agarista revoluta"     "Amaioua intermedia"    "Baccharis reticularia"

stringr . ,

library(stringr)
word(x, 1, 2)
#[1] "Agarista revoluta"     "Amaioua intermedia"    "Baccharis reticularia"
+3

:

with(read.table(text = m, fill = TRUE), trimws(paste(V1, V2)))

:

[1] "Agarista revoluta"     "Amaioua intermedia"    "Baccharis reticularia"

, trimws.

+2

"", 2

x <- c("Agarista revoluta (Spreng.) Hook. f. ex Nied.", "Amaioua intermedia Mart.", 
       "Baccharis reticularia DC.")
sapply(x, function(y) paste(unlist(strsplit(y, " "))[1:2], collapse = " "))
+1

Source: https://habr.com/ru/post/1664429/


All Articles