gsub

How can I extract from title from name in a column?

ぃ、小莉子 提交于 2019-12-11 20:29:03
问题 I have a column of names of the form "Hobs, Mr. jack" i.e. lastname, title. firstname. title has 4 types -"Mr.", "Mrs.","Miss.","Master." How can I search for each item in the column & return the title ,which I can store in another column ? Name <- c("Hobs, Mr. jack","Hobs, Master. John","Hobs, Mrs. Nicole",........) desired output - a column "title" with values - ("Mr","Master", "Mrs",.....) I have tried something like this: f <- function(d) { if (grep("Mr", d$title)) { gsub("$Mr$", "Mr", d

replace tab in an enclosed string in a tab delimited file linux

时间秒杀一切 提交于 2019-12-11 10:18:31
问题 I have a tab delimited txt file in which third column contains enclosed string that might also has a tab. Because of this extra tab i am getting 5 columns when i try to read this tab delimited file. So i want to replace the tab with space. Following is the sample file. col1 col2 col3 col4 1 abc "pqr xyz" asd 2 asd "lmn pqr" aws 3 abc "asd" lmn I want the output like this col1 col2 col3 col4 1 abc "pqr xyz" asd 2 asd "lmn pqr" aws 3 abc "asd" lmn Here is what i have tried awk -F"\t" '{ gsub("

How to replace multiple substrings with same string using `gsub`

蹲街弑〆低调 提交于 2019-12-11 10:14:35
问题 I want to change different charaters/substrings to a single character or nil . I want to change "How to chop an onion?" to "how-chop-onion" . string .gsub(/'s/,'') .gsub(/[?&]/,'') .gsub('to|an|a|the','') .split(' ') .map { |s| s.downcase} .join '-' Using pipe character | does not work. How can I do this with gsub ? 回答1: to|an|a|the is pattern, you are using it as String. Here: str.gsub('to|an|a|the', '') # passing string argument #=> "How to chop an onion?" str.gsub(/to|an|a|the/, '') #

Remove character from string in R

帅比萌擦擦* 提交于 2019-12-11 08:03:28
问题 I have a data frame as given below: data$Latitude "+28.666428" "+28.666470" "+28.666376" "+28.666441" "+28.666330" "+28.666391" str(data$Latitude) Factor w/ 1368 levels "+10.037451","+10.037457",.. I want to remove the "+" character from each of the Latitude values. I tried using gsub() data$Latitude<-gsub("+","",as.character(factor(data$Latitude))) This isn't working. 回答1: You can use a combination of sapply , substring and regexpr to achieve the result. regexpr(<character>,<vector>)[1]

Replace the string value with value in the find list in R

我是研究僧i 提交于 2019-12-11 07:27:30
问题 I have a dataset that has a column like string<-c('lib1_Rstudio_case1','lib2_Rstudio_case1and2','lib5_python_notthe correct_language','lib3_Jupyter_really_good','lib1_spyder_nice','lib1_R_the_core') replacement<-c('Rstudio','Jupyter','spyder','R') I want to replace the string value id they match the value in replacement. I am using the following code right now gsub(paste(replacement, collapse = "|"), replacement = replacement, x = string) This in another piece of code which i am using to find

R RegEx: Match all double-quote (") characters inside square brackets

半城伤御伤魂 提交于 2019-12-11 03:08:57
问题 I'm struggling to get a RegEx expression that matches all double-quote characters ( " ) that occur within square brackets. I have different pieces that do parts of what I want. For example, gsub('"', "", '"""xyz"""') [1] "xyz" Will get all double-quotes, irrespective of anything else. gsub('\\[(.*?)\\]', "", '[xyz][][][]abc') [1] "abc" Will get everything inside two square brackets, including the brackets themselves ( which I do not want to happen -- how do I avoid that? ). I'm also not sure

R - How to split text and punctuation with a exception?

送分小仙女□ 提交于 2019-12-11 03:06:17
问题 Analysing Facebook comments in R for Sentimental Analysis. Emojis are coding in text between <> symbols. Example: "Jesus te ama!!! <U+2764> Ou não...?<U+1F628> (fé em stand by)" <U+2764> and <U+1F628> are emojis (heavy black heart and fearful face, respectively). So, I need split words/numbers and punctuations/symbols, except in emoji codes. I did, using gsub function, this: a1 <- "([[:alpha:]])([[:punct:]])" a2 <- "([[:punct:]])([[:alpha:]])" b <- "\\1 \\2" gsub(a1, b, gsub(a2, b, "Jesus te

How to gsub('%', '\%', … in R?

China☆狼群 提交于 2019-12-11 02:37:56
问题 I want to export a latex table with a units column that has the percent (%) symbol. library(xtable) foo <- data.frame(units='%', citation = '\\citep{authorYYYYabc}') print(xtable(foo), sanitize.text.function = function(x) {x}) note: above code has been changed since Joris' answer. In this case, the '%' is interpreted as a comment by LaTeX. I have tried gsub('%', '\\%', foo) returns [1] "1" how can I convert the % to \% so that LaTex comments it out? This question is a little bit like a

R: gsub and capture

青春壹個敷衍的年華 提交于 2019-12-11 01:53:35
问题 I am trying to extract the contents between square brackets from a string: eq <- "(5) h[m] + nadh[m] + q10[m] --> (4) h[c] + nad[m] + q10h2[m]" I can filter them out: gsub("\\[.+?\\]","" ,eq) ##replaces square brackets and everything inside it [1] "(5) h + nadh + q10 --> (4) h + nad + q10h2" But how can I capture what's inside the brackets? I tried the following: gsub("\\[(.+)?\\])", "\\1", eq) grep("\\[(.+)?\\]", eq, value=TRUE) but both return me the whole string: [1] "(5) h[m] + nadh[m] +

Trying to avoid for loop with sapply (for gsub)

本秂侑毒 提交于 2019-12-10 21:22:14
问题 Trying to avoid using a for loop in the following code by utilizing sapply , if at all possible. The solution with loop works perfectly fine for me, I'm just trying to learn more R and explore as many methods as possible. Objective: have a vector i and two vectors sf (search for) and rp (replace). For each i need to loop over sf and replace with rp where match. i = c("1 6 5 4","7 4 3 1") sf = c("1","2","3") rp = c("one","two","three") funn <- function(i) { for (j in seq_along(sf)) i = gsub(sf