FAQ Database Discussion Community


replace string in R giving a vector of patterns and vector of replacements

r,stringr,qdap
Given a string with different placeholders I want to replace, does R have a function that replace all of them given a vector of patterns and a vector of replacements? I have managed to accomplish that with a list and a loop > library(stringr) > tt_ori <- 'I have [%VAR1%]...

String matching records to count all instances in a dataframe

r,reshape2,stringr,agrep
I am trying to extract all strings from rows in a dataframe that match certain criteria for example how many words are match 'corn' in each row. Here is the input. install.packages('stringr') library(stringr) dataset <- c("corn", "cornmeal", "corn on the cob", "meal") y<- c('corn',"corn","mean","meal") id<- c(1,2,3,4) dataset <- data.frame(id,dataset,y) id...

Extract text in parentheses in R

r,string,text,vector,stringr
Two related questions. I have vectors of text data such as "a(b)jk(p)" "ipq" "e(ijkl)" and want to easily separate it into a vector containing the text OUTSIDE the parentheses: "ajk" "ipq" "e" and a vector containing the text INSIDE the parentheses: "bp" "" "ijkl" Is there any easy way to...

Identify continuously occurring stretch of specific letters in a string using R

r,substr,stringr
I would like to identify if the string column in the data frame below repeats the letters "V" or "G" at least 5 times within the first 20 characters of the string. Sample data: data = data.frame(class = c('a','b','C'), string = c("ASADSASAVVVVGVGGGSDASSSDDDFGDFGHFGHFGGGGGDDFFDDFGDFGTYJ", "AWEERTGVTHRGEFGDFSDFSGGGGGGDAWSDFAASDADAADWERWEQWD", "GRTVVGGVVVGGSWERGERVGEGDDFASDGGVQWEQWEQWERERYRYER")) For example the string in the...

Re-model the shape of my data.frame/excel spreadsheet

r,data.frame,structure,gsub,stringr
this is my first question on this forum. I would like to re-model the structure of my dataset. I would like to split the column "Teams" into two columns. One with the hometeam and another with the awayteam. I also would like to split the result into two columns. Homegoals...

Regular expression in r. Grouping & Capturing

regex,r,stringr
I'm trying to use regexp in R cran, using the library stringr. I was studing str_match and str_replace functions. I don't understand why they give different results when I use parentheses for Grouping : library(stringr) s<-"(.+?)( PIAZZALE | SS)(.+?)([0-9]{5})" a<-str_match("MONT SS DPR PIAZZALE CADORNA, 1A RICCIONE 47838",perl(s)) b<-str_replace("MONT SS DPR...

Regex and file processing

regex,r,stringr,metacharacters
This question relates to R but really isn't language specific per se. I have a bunch of csv files with this general format "sitename_03082015.csv". The files have 5 columns and various rows Host MaximumIn MaximumOut AverageIn AverageOut device1 30.63 Kbps 0 bps 24.60 Kbps 0 bps device2 1.13 Mbps 24.89...

Extract last 4-digit number from a series in R using stringr

regex,r,stringr
I would like to flatten lists extracted from HTML tables. A minimal working example is presented below. The example depends on the stringr package in R. The first example exhibits the desired behavior. years <- c("2005-", "2003-") unlist(str_extract_all(years,"[[:digit:]]{4}")) [1] "2005" "2003" The below example produces an undesirable result when I...

How to delete a row, having string which start with a white space?

r,stringr
my dataset is like the following John Tally mac hero I want to delete the string which start with " " so the resulting variable would be John Tally hero I have used library(stringr) which(startsWith(names[,1]," ")) to get the rows having " " Please help me any efficient way to...

Remove part of the string in json document using str replace for many records

regex,r,string-matching,stringr
I would like to replace a string in this file which is causing the invalid json arguments. I can manually delete the first string "_id" : ObjectId( "539163d7bd350003" ), and can convert this json to a data frame. Is there a way I can replace all the instances of json...

Regex to remove .csv in r

regex,r,stringr
this is going to be silly. I have a string like: word <- "dirtyboards.csv" I want to remove the csv part and get "dirtyboards". I am trying: require(stringr) str_extract(word, ".*[^.csv]") I get in return: "dirtyboard" . The "s" before the ".csv" goes missing. I know I can do , gsub(".csv",...

Isolating specific numeric values in text

r,grep,stringr
I have a significant amount of text to trawl through on a text csv file of various properties to find what the square metre numeric value of the properties actually is. For example: string <- "This is a wonderful 120 sqm flat with a stunning view" I know that I...

splitting a string in which upper case follows lower case in stringr

regex,r,stringr
I have a string vector that looks like this and I'd like to split it up: str <- c("Fruit LoopsJalapeno Sandwich", "Red Bagel", "Basil LeafBarbeque SauceFried Beef") str_split(str, '[a-z][A-Z]', n = 3) [[1]] [1] "Fruit Loop" "alapeno Sandwich" [[2]] [1] "Red Bagel" [[3]] [1] "Basil Lea" "arbeque Sauc" "ried Beef"...