stringr

An error I can't understand. “Promise already under evaluation…” [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 09:52:13
问题 This question already has answers here : promise already under evaluation: recursive default argument reference or earlier problems? (3 answers) Closed 6 years ago . I am trying to write a function that finds pattern in names, with the help of stringr package. My function looks like following: namezz=function(thepatternx,data=data,column=Name){ library(stringr) thepattern=as.character(quote(thepatternx)) pattern <- thepattern strings <- data$column ##data$column is a character vector found

Filter by multiple patterns with filter() and str_detect()

孤街浪徒 提交于 2019-12-01 08:11:37
I would like to filter a dataframe using filter() and str_detect() matching for multiple patterns without multiple str_detect() function calls. In the example below I would like to filter the dataframe df to show only rows containing the letters a f and o . df <- data.frame(numbers = 1:52, letters = letters) df %>% filter( str_detect(.$letters, "a")| str_detect(.$letters, "f")| str_detect(.$letters, "o") ) # numbers letters #1 1 a #2 6 f #3 15 o #4 27 a #5 32 f #6 41 o I have attempted the following df %>% filter( str_detect(.$letters, c("a", "f", "o")) ) # numbers letters #1 1 a #2 15 o #3 32

How do I extract a file/folder_name only from a path?

会有一股神秘感。 提交于 2019-12-01 06:34:30
Unfortunately I suck at regexp. If I have a path like so: /long/path/to/file , I just need to extact file . If someone supplies file/ I just need file . If someone supplies /file/ , I still need just file . I've been using stringr functions as a crutch but this seems like straight up grep territory. Help, please? If I understand correctly, you could use the basename function. f <- "/long/path/to/file" basename(f) # [1] "file" What about this? > path <- "/long/path/to/file" > require(stringr) > str_extract(path, "[^/]*$") [1] "file" 来源: https://stackoverflow.com/questions/9693877/how-do-i

str_replace (package stringr) cannot replace brackets in r?

孤者浪人 提交于 2019-12-01 04:36:47
I have a string, say fruit <- "()goodapple" I want to remove the brackets in the string. I decide to use stringr package because it usually can handle this kind of issues. I use : str_replace(fruit,"()","") But nothing is replaced, and the following is replaced: [1] "()good" If I only want to replace the right half bracket, it works: str_replace(fruit,")","") [1] "(good" However, the left half bracket does not work: str_replace(fruit,"(","") and the following error is shown: Error in sub("(", "", "()good", fixed = FALSE, ignore.case = FALSE, perl = FALSE) : invalid regular expression '(',

How do I extract a file/folder_name only from a path?

ぐ巨炮叔叔 提交于 2019-12-01 03:45:48
问题 Unfortunately I suck at regexp. If I have a path like so: /long/path/to/file , I just need to extact file . If someone supplies file/ I just need file . If someone supplies /file/ , I still need just file . I've been using stringr functions as a crutch but this seems like straight up grep territory. Help, please? 回答1: If I understand correctly, you could use the basename function. f <- "/long/path/to/file" basename(f) # [1] "file" 回答2: What about this? > path <- "/long/path/to/file" > require

str_replace (package stringr) cannot replace brackets in r?

大城市里の小女人 提交于 2019-12-01 02:44:15
问题 I have a string, say fruit <- "()goodapple" I want to remove the brackets in the string. I decide to use stringr package because it usually can handle this kind of issues. I use : str_replace(fruit,"()","") But nothing is replaced, and the following is replaced: [1] "()good" If I only want to replace the right half bracket, it works: str_replace(fruit,")","") [1] "(good" However, the left half bracket does not work: str_replace(fruit,"(","") and the following error is shown: Error in sub("(",

Filter by multiple patterns with filter() and str_detect()

隐身守侯 提交于 2019-11-30 17:25:21
问题 I would like to filter a dataframe using filter() and str_detect() matching for multiple patterns without multiple str_detect() function calls. In the example below I would like to filter the dataframe df to show only rows containing the letters a f and o . df <- data.frame(numbers = 1:52, letters = letters) df %>% filter( str_detect(.$letters, "a")| str_detect(.$letters, "f")| str_detect(.$letters, "o") ) # numbers letters #1 1 a #2 6 f #3 15 o #4 27 a #5 32 f #6 41 o I have attempted the

R count times word appears in element of list

爱⌒轻易说出口 提交于 2019-11-29 12:28:26
I have a list comprised of words. > head(splitWords2) [[1]] [1] "Some" "additional" "information" "that" "we" "would" "need" "to" "replicate" "the" [11] "experiment" "is" "how" "much" "vinegar" "should" "be" "placed" "in" "each" [21] "identical" "container" "or" "what" "tool" "use" "measure" "mass" "of" "four" [31] "different" "samples" "and" "distilled" "water" "rinse" "after" "taking" "them" "out" [[2]] [1] "After" "reading" "the" "expirement" "I" "realized" "that" "additional" "information" "you" [11] "need" "to" "replicate" "expireiment" "is" "one" "amant" "of" "vinegar" "poured" [21] "in"

Regular Expression in Base R Regex to identify email address

末鹿安然 提交于 2019-11-29 12:25:02
I am trying to use the stringr library to extract emails from a big, messy file. str_match doesn't allow perl=TRUE, and I can't figure out the escape characters to get it to work. Can someone recommend a relatively robust regex that would work in the context below? c("larry@gmail.com", "larry-sally@sally.com", "larry@sally.larry.com")->emails "SomeRegex"->regex str_match(emails, regex) 42- > "^[[:alnum:].-_]+@[[:alnum:].-]+$"->regex > str_match(emails, regex) [,1] [1,] "larry@gmail.com" [2,] "larry-sally@sally.com" [3,] "larry@sally.larry.com" The @-sign is not in need of escaping in regex.

Extract part of string between two different patterns

橙三吉。 提交于 2019-11-29 09:49:00
问题 I try to use stringr package to extract part of a string, which is between two particular patterns. For example, I have: my.string <- "nanaqwertybaba" left.border <- "nana" right.border <- "baba" and by the use of str_extract(string, pattern) function (where pattern is defined by a POSIX regular expression ) I would like to receive: "qwerty" Solutions from Google did not work. 回答1: I do not know whether and how this is possible with functions provided by stringr but you can also use base