Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying all vectors without the given character (#)

Tags:

regex

r

I know that there is a similar topic on the forum, but there is no answer to my question, and I tried in different ways (R regex - extract words beginning with @ symbol). I need to write out all words that do not have a symbol in front of them.

Below is a code that cuts out all words that contain the #sign and the result of this action.

tweeter <- c("#tweeter tweet", "h#is", "tweet #tweeter2", "twet")
str_extract_all(tweeter, "(?<=\\B\\#)[^\\s]+")

Result of it:

[[1]]
[1] "tweeter"

[[2]]
character(0)

[[3]]
[1] "tweeter2"

[[4]]
character(0)

Now the code with which he tries to display all the words without # at the beginning.

regmatches(tweeter, gregexpr("\\B#\\S+", tweeter), invert = T) 

I would like to display a list of words that don't start with #just don't know how to write it properly.

like image 621
Jakub Bidziński Avatar asked Oct 05 '19 13:10

Jakub Bidziński


People also ask

How do you remove a character from a vector?

clear() removes all elements from vector and reducing it to size 0. erase() is used to remove specific elements from vector.

How do I print a vector without loop?

Printing all elements without for loop by providing element type: All the elements of a vector can be printed using an STL algorithm copy(). All the elements of a vector can be copied to the output stream by providing elements type while calling the copy() algorithm.

How do I show a vector in C++?

In the for loop, size of vector is calculated for the maximum number of iterations of loop and using at(), the elements are printed. for(int i=0; i < a. size(); i++) std::cout << a.at(i) << ' '; In the main() function, the elements of vector are passed to print them.


2 Answers

This gives you words that doesn't begin with #

library(stringr)

tweeter[!str_detect(tweeter, "^#")]
# "h#is"            "tweet #tweeter2" "twet"  

Explanation

str_detect(tweeter, "^#") returns a logical vector based on the pattern and in this case ^#. ^ matches the beginning and # specifies the character to match at the beginning.

Finally, to return values where the condition is TRUE we use tweeter[].

The same thing can be achieved without using ! which negates the logical value by using negate argument within the str_detect as follows:

tweeter[str_detect(tweeter, "^#", negate = TRUE)]
# "h#is"            "tweet #tweeter2" "twet" 
like image 163
deepseefan Avatar answered Sep 30 '22 18:09

deepseefan


In base R, we can use grep with invert and value parameter as TRUE.

grep("^#", tweeter, invert = TRUE, value = TRUE)
#[1] "h#is"            "tweet #tweeter2" "twet" 
like image 28
Ronak Shah Avatar answered Sep 30 '22 16:09

Ronak Shah