'Using ggplot to plot number of TRUE statements from a df
I'm trying to plot a graph where number of TRUE statement from a df column.
I have a df that looks like this
Speed Month_1
12 67
12 114
12 155
12 44
13 77
13 165
13 114
13 177
...
And I would like to plot a bargraph where we have x = Speed and y = Number of rows that are above 100 in Month_1 column.
So for X = 12 I would have a bargraph with a Y-value of 2 and for X = 13 I would have a Y-value of 3.
Can I do this directly in ggplot, or do I have to create a new DF first?
Solution 1:[1]
You can use dplyr to filter your data frame and then plot it with ggplot.
library(tidyverse)
df <- tibble(Speed = c(12, 12, 12, 12, 13, 13, 13, 13),
Month_1 = c(67, 114, 155, 44, 77, 165, 114, 177))
df %>% filter(Month_1 > 100) %>%
ggplot(aes(x = Speed)) + geom_bar()
Solution 2:[2]
Bill's answer is good but may you like this one: just find all words with test and then filter out useless ones;
const s = "Hello this is testing _testisgood _test test ilovetesting again test"
console.log(
(s.match(/[^\s]*test[^\s]*/gi) || []).filter(s => s !== 'test')
)
Solution 3:[3]
The regex below will match 'test' when it either has a non-whitespace character(s) prefixing or post fixing it.
/([^\s]+test[^\s]*|[^\s]*test[^\s]+)/gi;
OR
/(\S+test\S*|\S*test\S+)/gi;
const sentence = "Hello this is testing _testisgood _test test ilovetesting again test";
regex = /([^\s]+test[^\s]*|[^\s]*test[^\s]+)/gi;
console.log(sentence.match(regex));
Solution 4:[4]
You can match just _testisgood and ilovetesting in your example by specifying one or more characters that are not whitespace before and after test, like this:
/[^\s]+test[^\s]+/gi
If you also want to match testing, then drop [^\s]+ from the beginning of the pattern.
Solution 5:[5]
should still work like this \btest\w+|\w+test\b|\w+test\B\w+
Solution 6:[6]
Since a "word" in your scenario is a chunk of one or more non-whitespace chars (and a non-whitespace char is matched with \S in regex) you can use
console.log(
"Hello this is testing _testisgood _test test ilovetesting again test"
.match(/\S+test\S*|\S*test\S+/gi))
// => ["testing","_testisgood", "_test", "ilovetesting"]
Here, \S+test\S*|\S*test\S+ (see this regex demo) matches either
\S+test\S*- one or more non-whitespace chars,testand zero or more non-whitespace chars|- or\S*test\S+- zero or more non-whitespace chars,testand one or more non-whitespace chars.
Or, you can split with any one or more whitespace chars (with .split(/\s+/)) and then filter out any chunk that is equal to test string or does not contain test string:
console.log(
"Hello this is testing _testisgood _test test ilovetesting again test".split(/\s+/)
.filter(x => x.toLowerCase() != "test" && x.toLowerCase().includes("test")))
Considering your expected output it may seem that you want to match any non-whitespace chunk that contains test and at least one more letter. In that case, you can use
console.log(
"Hello this is testing _testisgood _test test ilovetesting again test"
.match(/[^\sa-zA-Z]*[a-zA-Z]\S*test\S*|\S*test[^\sa-zA-Z]*[a-zA-Z]\S*/gi))
// => ["testing","_testisgood", "_test", "ilovetesting"]
See this regex demo. Output: ["testing", "_testisgood", "ilovetesting"]
Details:
[^\sa-zA-Z]*- any zero or more chars other than whitespace and letters[a-zA-Z]- a letter\S*test\S*-testenclosed with zero or more non-whitespace chars|- or\S*test[^\sa-zA-Z]*[a-zA-Z]\S*- zero or more non-whitespace chars,test, any zero or more chars other than whitespace and letters and then a letter and zero or more non-whitespace chars.
Solution 7:[7]
If what you want is:
Gives all the test but I only want the test string which is surrounded by some other character
Let's do it with another approach string built-in functions, instead of Regex
function surroundedBy(string, wordInMiddle){
const allWords = s.split(/\s+/);
const wordsSurrounded = allWords.filter(word=>word.toLowerCase().includes(wordInMiddle) &&
!word.startsWith(wordInMiddle) &&
!word.endsWith(wordInMiddle))
return wordsSurrounded;
}
Test:
const s = "Hello this is testing _testisgood _test test ilovetesting again test"
console.log(surroundedBy(s,'test'))
Result: ['_testisgood', 'ilovetesting']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PhJ |
| Solution 2 | Mike |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | zaha |
| Solution 6 | |
| Solution 7 | TAHER El Mehdi |
