gsub

Changing text based on the final letter of user name using regular expression

落花浮王杯 提交于 2019-12-02 02:26:11
问题 I am looking to change the ending of the user name based on the use case (in the language system will operate, names ends depending on how it is used). So need to define all endings of names and define the replacement for them. Was suggested to use .gsub regular expression to search and replace in a string: Changing text based on the final letter of user name "name surname".gsub(/e\b/, 'ai') this will replace e with ai, so "name surname = namai surnamai". How can it be used for more options

Removing characters after a EURO symbol in R

烂漫一生 提交于 2019-12-02 01:58:27
I have a euro symbol saved in "euro" variable: euro <- "\u20AC" euro #[1] "€" And "eurosearch" variable contains "services as defined in this SOW at a price of € 15,896.80 (if executed fro" . eurosearch [1] "services as defined in this SOW at a price of € 15,896.80 (if executed fro" I want the characters after the Euro symbol which is "15,896.80 (if executed fro" I am using this code: gsub("^.*[euro]","",eurosearch) But I'm getting empty result. How can I obtain the expected output? Wiktor Stribiżew You can use variables in the pattern by just concatenating strings using paste0 : euro <- "€"

how to replace nth character of a string in a column in r

此生再无相见时 提交于 2019-12-02 00:57:14
问题 My input is a<-c("aa_bbb_cc_ddd","ee_fff_gg_hhh") b<-c("a","b") df<-data.frame(cbind(a,b)) I want my output to be a<-c("aa_bbb-cc_ddd","ee_fff-gg_hhh") b<-c("a","b") df<-data.frame(cbind(a,b)) please help 回答1: You may use sub, sub("^([^_]*_[^_]*)_", "\\1-",df$a) Example: > a<-c("aa_bbb_cc_ddd","ee_fff_gg_hhh") > b<-c("a","b") > df<-data.frame(cbind(a,b)) > df a b 1 aa_bbb_cc_ddd a 2 ee_fff_gg_hhh b > df$a <- sub("^([^_]*_[^_]*)_", "\\1-",df$a) > df a b 1 aa_bbb-cc_ddd a 2 ee_fff-gg_hhh b 回答2:

how to replace nth character of a string in a column in r

人盡茶涼 提交于 2019-12-01 22:24:09
My input is a<-c("aa_bbb_cc_ddd","ee_fff_gg_hhh") b<-c("a","b") df<-data.frame(cbind(a,b)) I want my output to be a<-c("aa_bbb-cc_ddd","ee_fff-gg_hhh") b<-c("a","b") df<-data.frame(cbind(a,b)) please help You may use sub, sub("^([^_]*_[^_]*)_", "\\1-",df$a) Example: > a<-c("aa_bbb_cc_ddd","ee_fff_gg_hhh") > b<-c("a","b") > df<-data.frame(cbind(a,b)) > df a b 1 aa_bbb_cc_ddd a 2 ee_fff_gg_hhh b > df$a <- sub("^([^_]*_[^_]*)_", "\\1-",df$a) > df a b 1 aa_bbb-cc_ddd a 2 ee_fff-gg_hhh b If things are as consistent as you show and you want to replace the 7th character then substring may be a good

Escaping Angled Bracket acts similar to look-ahead

拜拜、爱过 提交于 2019-12-01 21:08:14
Why does escaping escaping the angled bracket > exhibit the look-ahead like behavior? To be clear, I understand that the angled bracket does not necessitate being escaped. The question is, how is the pattern being interpreted that it yields the match(es) shown ## match bracket, with or without underscore ## replace with "greater_" strings <- c("ten>eight", "ten_>_eight") repl <- "greater_" ## Unescaped. Yields desired results gsub(">_?", repl, strings) # [1] "tengreater_eight" "ten_greater_eight" ## All four of these yield the same result gsub("\\>_?", repl, strings) # (a) gsub("\\>(_?)", repl

r code removing words containing @

血红的双手。 提交于 2019-12-01 20:26:18
I want to replace all words containing the symbol @ with a specific word. I am used gsub and therefore am applying it to a character vector. The issue that keeps occuring is that when I use: gsub(".*@.*", "email", data) all of the text in that portion of the character vector gets deleted. There are multiple different emails all with different lengths so I can't set the characters prior and characters after to a specific number. Any suggestions? I've done my fair share of reading about regex but everything I tried failed. Here's an example: data <- c("This is an example. Here is my email:

Removing parenthesis in R

被刻印的时光 ゝ 提交于 2019-12-01 19:32:04
I am trying to remove parentheses from a string value in this case this one: (40.703707008, -73.943257966) I can't seem to find a post with code that works; I know that this is a very simple task, but I've seen the following links but they either kill all my punctuation or don't seem to work. Below is the codes I've tried. Appreciate the help: remove parenthesis from string Remove parentheses and text within from strings in R x = ("(40.703707008, -73.943257966)") gsub("\\s*\\([^\\)]+\\)","",x) gsub("\\D", "", x) gsub("log\\(", "", x) These are metacharacters that either needs to be escaped

get filename from url path in R

≡放荡痞女 提交于 2019-12-01 17:54:47
I would like to extract filename from url in R. For now I do it as follows, but maybe it can be done shorter like in python. assuming path is just string. path="http://www.exanple.com/foo/bar/fooXbar.xls" in R: tail(strsplit(path,"[/]")[[1]],1) in Python: path.split("/")[-1:] Maybe some sub, gsub solution? There's a function for that... basename(path) [1] "fooXbar.xls" @SimonO101 has the most robust answer IMO, but some other options: Since regular expressions are greedy, you can use that to your advantage sub('.*/', '', path) # [1] "fooXbar.xls" Also, you shouldn't need the [] around the / in

Duplicating observations of a dataframe, but also replacing specific variable values in R

徘徊边缘 提交于 2019-12-01 16:41:25
I am looking for some advice on some data restructuring. I am collecting some data using Google Forms which I download as a csv file and looks something like the following: # alpha beta option # 6 8, 9, 10, 11 apple # 9 6 pear # 1 6 apple # 3 8, 9 pear # 3 6, 8 lime # 3 1 apple # 2, 4, 7, 11 9 lime The data has two variables (alpha and beta) that each list numbers. For the majority of my data there is only one number in each variable. However, for some observations there can be two, three or even up to ten numbers. This is because these are responses gathered using the 'checkbox' option in

R gsub a single double quotation mark

こ雲淡風輕ζ 提交于 2019-12-01 16:27:56
I have a field of strings in a data frame all similar to: "Young Adult – 8-9"" where the inner single " is what I want to replace with nothing to get: "Young Adult - 8-9" How can I do this? I tried to escape with a double backslash: gsub("\\"", "", string) but got this error: Error: unexpected string constant in "gsub("\"", "" You do not need to escape a double quote in a regular expression. Just use "\"" or '"' to match a single double quote. s = "Young Adult – 8-9\"" s [1] "Young Adult – 8-9\"" gsub("\"", "", s) [1] "Young Adult – 8-9" gsub('"', "", s) [1] "Young Adult – 8-9" See this IDEONE