FAQ Database Discussion Community


How to perl regex match in R in the grepl function?

r,function,pattern-matching,pcre,grepl
I have a function in R which uses the grepl command as follows: function(x) grepl('\bx\b',res$label, perl=T) This doesn't seem to work - the 'x' input is a character type string (a sentence), and i'd like to create word boundaries around the 'x' as I match, as I don't want the...

How do I match table entries to a second table's list entries in R?

r,loops,for-loop,grepl
I have two tables, table1 has a column of strings. I'd like to find the matches of each string in another table, table2, but in the corresponding table2 column, each cell contain lists per row entry. So far, I have figured out how to use grepl to match particular entries:...

Using grepl in R to match string

r,sapply,grepl
I have a frame data "testData" as follows: id content 1 I came from China 2 I came from America 3 I came from Canada 4 I came from Japan 5 I came from Mars And I also have another frame data "addr" as follows: id addr 1 America 2...

pattern matching in R using grepl

regex,r,grepl
I have a dataframe dat like this P pedigree cas 1 M rs2745406 T 2 M rs6939431 A 3 M SNP_DPB1_33156641 G 4 M SNP_DPB1_33156664_G P 5 M SNP_DPB1_33156664_A A 6 M SNP_DPB1_33156664_T A I want to exclude all rows where the pedigree column starts with SNP_ and ends with...

Grepl first letters, two patterns to match

r,grepl
I have the following data frame. I want to create a column called A1B1 with a 1 if there are strings starting with A1 or B1 or both in the data frame and a zero if not. What am I doing wrong here: set.seed(1) Data <- data.frame(id = seq(1, 10),...

r grepl search for multiple patterns in a variable [duplicate]

regex,r,pattern-matching,grepl
This question already has an answer here: matching a string with different possibilities using grep 3 answers I'm trying to search into a string variable and every time a determinate pattern is found the search funtion tell me TRUE. I'm using grepl to find the match: grepl(pattern,x) The pattern...

Multiple filter using grep and subset in R

r,grep,subset,grepl
I'm trying to create a filter to remove lines from a dataset using grep and subset together. Sample dataset: id <- 1:10 problem <- c("a" , "b", "c", "d", "a","b","c","a", "b", "a") solution1 <- c("eat", "sleep", "drink", "play", "sleep", "play", "play", "drink", "play", "eat") solution2 <- c("read", "read", "eat", "drink",...

Using grepl in R

r,grepl
In which cases could these 2 different ways of implimentation would give different results? data(mtcars) firstWay <- mtcars[grepl('6',mtcars$cyl),] SecondWay <- mtcars[mtcars$cyl=='6',] If these ways always give the same results which of them is recommended and why? Thanks...

Summarizing across multiple search strings in R using grepl and dplyr

r,dplyr,grepl
I have a rather unorganized dataframe which has varying names for the same categories in one column. I want to summarize across those messy names using dplyr. Here's a simplified dataset, of tree species and their traits: df <- data.frame(species = c('sp1', 'sp1', 'sp1', 'sp2', 'sp2'), tr = c('leaf nitrogen...

grepl in R: spurious match despite intra-word dash

r,grepl
below is a minimal reproducible example: v=c("\\<skill-saw\\>","\\<saw blade\\>") text="xx placed his hand beneath skill-saw blade" sapply(v,grepl,text) The last command returns c(TRUE,TRUE) where I was expecting c(TRUE,FALSE). Any idea on how to achieve that? The idea is that the keyword "skill-saw" should be detected as present in the text, but not...

grepl in R: matching impeded by intra-word dashes

regex,r,lookahead,lookbehind,grepl
I have 3 words: x, y, and z, from which two compound words can be built: x-y, and y-z. In naturally occuring text, x, y, and z can follow each other. In the first case, I have: text="x-y z" And I want to detect: "x-y" but not "y z". If...

In R, use regular expression to match multiple patterns and add new column to list

regex,r,grepl
I've found numerous examples of how to match and update an entire list with one pattern and one replacement, but what I am looking for now is a way to do this for multiple patterns and multiple replacements in a single statement or loop. Example: > print(recs) phonenumber amount 1...

R : Changing variable names with a for(); grepl() and colnames()

r,variable-names,grepl
Im trying to make a data panel and in the way i have to rename some columns in order to apply rbind() and then run some statistics. i have multiple objects like this: data22 1. year -- id-----22.Letters.spendingXX -----22.Letters.IncomeXX 2. 1999 -- 22 ------ 21 ---------------------------- 52 3. 2000 --...

R how to use grep in if statement

r,if-statement,grepl
In R I want to do a like in an if statement like the example below where I'm searching for any colors in the mix$color column that contain the word red and setting a new variable in the mix dataframe to the color red. mix$newcolor <- if(grep("Red",mix$color) "red" And here's...