gsub

Extracting unique numbers from string in R

て烟熏妆下的殇ゞ 提交于 2019-11-26 18:52:12
I have a list of strings which contain random characters such as: list=list() list[1] = "djud7+dg[a]hs667" list[2] = "7fd*hac11(5)" list[3] = "2tu,g7gka5" I'd like to know which numbers are present at least once ( unique() ) in this list. The solution of my example is: solution: c(7,667,11,5,2) If someone has a method that does not consider 11 as "eleven" but as "one and one", it would also be useful. The solution in this condition would be: solution: c(7,6,1,5,2) (I found this post on a related subject: Extracting numbers from vectors of strings ) For the second answer, you can use gsub to

Remove strings found in vector 1, from vector 2

旧巷老猫 提交于 2019-11-26 18:38:42
问题 I have these two vectors: sample1 <- c(".aaa", ".aarp", ".abb", ".abbott", ".abogado") sample2 <- c("try1.aarp", "www.tryagain.aaa", "255.255.255.255", "onemoretry.abb.abogado") I am trying to remove sample1 strings that are found in sample2. The closest I got is by iterating using sapply , which gave me this: sapply(sample1, function(i)gsub(i, "", sample2)) .aaa .aarp .abb .abbott .abogado [1,] "try1.aarp" "try1" "try1.aarp" "try1.aarp" "try1.aarp" [2,] "www.tryagain" "www.tryagain.aaa" "www

How to remove unicode <U+00A6> from string?

耗尽温柔 提交于 2019-11-26 17:52:24
问题 I have a string like: q <-"<U+00A6> 1000-66329" I want to remove <U+00A6> and get only 1000 66329 . I tried using: gsub("\u00a6"," ", q,perl=T) But it is not removing anything. How should I do gsub in order to get only 1000 66329 ? 回答1: I just want to remove unicode <U+00A6> which is at the beginning of string. Then you do not need a gsub , you can use a sub with "^\\s*<U\\+\\w+>\\s*" pattern: q <-"<U+00A6> 1000-66329" sub("^\\s*<U\\+\\w+>\\s*", "", q) Pattern details : ^ - start of string \

Replace characters using gsub, how to create a function?

﹥>﹥吖頭↗ 提交于 2019-11-26 17:50:20
I'm trying to replace characters in a data.frame. I have a solution for this > df <- data.frame(var1 = c("aabbcdefg", "aabbcdefg")) > df var1 1 aabbcdefg 2 aabbcdefg > df$var1 <- gsub("a", "h", df$var1) > df$var1 <- gsub("b", "i", df$var1) > df$var1 <- gsub("c", "j", df$var1) > df$var1 <- gsub("d", "k", df$var1) > df$var1 <- gsub("e", "l", df$var1) > df$var1 <- gsub("f", "m", df$var1) > df var1 1 hhiijklmg 2 hhiijklmg > but I would like to avoid using several gsub calls, it would be much nicer to produce a function to do this at once? Any ideas ho to proceed? You can create from and to vectors

Replace multiple strings in one gsub() or chartr() statement in R?

依然范特西╮ 提交于 2019-11-26 16:28:00
问题 I have a string variable containing alphabet[a-z], space[ ], and apostrophe['],eg. x <- "a'b c" I want to replace apostrophe['] with blank[], and replace space[ ] with underscore[_]. x <- gsub("'", "", x) x <- gsub(" ", "_", x) It works absolutely, but when I have a lot of condition, the code becomes ugly. Therefore, I want to use chartr() , but chartr() can't deal with blank, eg. x <- chartr("' ", "_", x) #Error in chartr("' ", "_", "a'b c") : 'old' is longer than 'new' Is there any way to

Ruby multiple string replacement

≡放荡痞女 提交于 2019-11-26 15:17:54
问题 str = "Hello☺ World☹" Expected output is: "Hello:) World:(" I can do this: str.gsub("☺", ":)").gsub("☹", ":(") Is there any other way so that I can do this in a single function call?. Something like: str.gsub(['s1', 's2'], ['r1', 'r2']) 回答1: Since Ruby 1.9.2, String#gsub accepts hash as a second parameter for replacement with matched keys. You can use a regular expression to match the substring that needs to be replaced and pass hash for values to be replaced. Like this: 'hello'.gsub(/[eo]/,

How to replace multiple strings with the same in R

喜欢而已 提交于 2019-11-26 14:49:03
问题 I have a string vec = c('blue','red','flower','bee') I want to convert different strings into the same in one line instead of seperately i.e. i could gsub blue and gsub red to make them both spell 'colour'. How can I do this in one line? output should be: 'colour','colour','flower','bee' 回答1: sub("blue|red", "colour", vec) use "|" (meening or) between the words you want to sub. use sub to change only the first occurence and gsub to change multiple occurences within the same string. See ?gsub

Ruby gsub doesn&#39;t escape single-quotes

微笑、不失礼 提交于 2019-11-26 14:12:38
问题 I don't understand what is going on here. How should I feed gsub to get the string "Yaho\'o"? >> "Yaho'o".gsub("Y", "\\Y") => "\\Yaho'o" >> "Yaho'o".gsub("'", "\\'") => "Yahooo" 回答1: \' means $' which is everything after the match. Escape the \ again and it works "Yaho'o".gsub("'", "\\\\'") 回答2: "Yaho'o".gsub("'", "\\\\'") Because you're escaping the escape character as well as escaping the single quote. 回答3: This will also do it, and it's a bit more readable: def escape_single_quotes(str)

Use gsub remove all string before first white space in R

[亡魂溺海] 提交于 2019-11-26 12:47:51
问题 I have a data frame like this: name weight r apple 0.5 y pear 0.4 y cherry 0.1 g watermelon 5.0 pp grape 0.5 y apple pear 0.4 ... ... I would like to remove all characters before the first white space in the name column. Can anybody give me a favor? Thank you! 回答1: Try this: sub(".*? ", "", D$name) Edit: The pattern is looking for any character zero or more times ( .* ) up until the first space, and then capturing the one or more characters ( (.+) ) after that first space. The ? after .*

Remove pattern from string with gsub

狂风中的少年 提交于 2019-11-26 09:39:34
问题 I am struggling to remove the substring before the underscore in my string. I want to use * (wildcard) as the bit before the underscore can vary: a <- c(\"foo_5\", \"bar_7\") a <- gsub(\"*_\", \"\", a, perl = TRUE) The result should look like: > a [1] 5 7 I also tried stuff like \"^* \" or \"? \" but did not really work. 回答1: The following code works on your example : gsub(".*_", "", a) 回答2: Alternatively, you can also try: gsub("\\S+_", "", a) 回答3: as.numeric(gsub(pattern=".*_", replacement