stringr

Finding the cause of an unwanted deletion within an lappy function

有些话、适合烂在心里 提交于 2019-12-25 01:12:03
问题 I uploaded a .txt file in to R as follows: Election_Parties <- readr::read_lines("Election_Parties.txt") The following text is in the file: pastebin link. The text more or less looks as follows (Please use actual file for solution!): BOLIVIA P1-Nationalist Revolutionary Movement-Free Bolivia Movement (Movimiento Nacionalista Revolucionario [MNR]) P19-Liberty and Justice (Libertad y Justicia [LJ]) P20-Tupak Katari Revolutionary Movement (Movimiento Revolucionario Tupak Katari [MRTK]) COLOMBIA

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

ε祈祈猫儿з 提交于 2019-12-24 06:14:05
问题 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%] and [%VAR2%]' > tt_out <- tt_ori > ttlist <- list('\\[%VAR1%\\]'="val-1", '\\[%VAR2%\\]'="val-2") > ttlist $`\\[%VAR1%\\]` [1] "val-1" $`\\[%VAR2%\\]` [1] "val-2" > for(var in names(ttlist)) { + print(paste0(var," -> ",ttlist

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

前提是你 提交于 2019-12-24 06:14:04
问题 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%] and [%VAR2%]' > tt_out <- tt_ori > ttlist <- list('\\[%VAR1%\\]'="val-1", '\\[%VAR2%\\]'="val-2") > ttlist $`\\[%VAR1%\\]` [1] "val-1" $`\\[%VAR2%\\]` [1] "val-2" > for(var in names(ttlist)) { + print(paste0(var," -> ",ttlist

Removing a pattern With gsub in r

元气小坏坏 提交于 2019-12-24 03:48:36
问题 I have a string Project Change Request (PCR) - HONDA DIGITAL PLATEFORM saved in supp_matches , and supp_matches1 contains the string Project Change Request (PCR) - . supp_matches2 <- gsub("^.*[supp_matches1]","",supp_matches) supp_matches2 # [1] " (PCR) - HONDA DIGITAL PLATEFORM" Which is actually not correct but it should come like supp_matches2 # [1] "HONDA DIGITAL PLATEFORM" Why is it not coming the way it should be? 回答1: As I say in my comment, in your expression gsub("^.*[supp_matches1]"

How to perform conditional search within a nested list

老子叫甜甜 提交于 2019-12-24 03:25:31
问题 I have a nested list as follows: list(c("Oesophagus irregular z-line as previously.", " quad biopsies at ,,,m" ), c("Normal examination", "cardia mild inflammation." ), c("stomach normal", "Small polyp EMR and completely removed", "Duodenum normal", "Jejunum normal", "GOJ normal", "Nodule seen at the mid oesophagus normal", "This was removed by EMR", "All other sites normal normal", " A small area of residual stomach was removed by APC ")) I would like to search in each element for the

Reorder words in each element of a vector

最后都变了- 提交于 2019-12-24 00:27:04
问题 I'd like to change the word order for each element in a vector. Specifically I'd like to make another vector where the first word is now the last word for a number of elements that differ in length. Data metadata1 <- c("reference1 an organism", "reference2 another organism here", "reference3 yet another organism is here") Desired outcome metadata2 <- c("an organism reference1", "another organism here reference2", "yet another organism is here reference3") My attempt metadata2 <- lapply

Using separate from tidyr with different length vectors

老子叫甜甜 提交于 2019-12-23 13:06:34
问题 I would like to separate a column of strings such as [1, 58, 10] into columns using separate from tidyr. My problem is that sometimes the columns are shorter (never longer). I have many columns with this issue in the same data frame. Loading packages require(tidyr) require(dplyr) require(stringr) The data Here I make a data frame with samples from the real data. The "vectors" are of length 10 in col1 and 9 or 10 in col2. There is a time column just to show that there are other columns as well

Using separate from tidyr with different length vectors

陌路散爱 提交于 2019-12-23 13:06:29
问题 I would like to separate a column of strings such as [1, 58, 10] into columns using separate from tidyr. My problem is that sometimes the columns are shorter (never longer). I have many columns with this issue in the same data frame. Loading packages require(tidyr) require(dplyr) require(stringr) The data Here I make a data frame with samples from the real data. The "vectors" are of length 10 in col1 and 9 or 10 in col2. There is a time column just to show that there are other columns as well

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

只愿长相守 提交于 2019-12-23 12:16:39
问题 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 file with function like str_replace. I tried the following but couldn't make it work. Any suggestions? library(RJSONIO) library(stringr) json_file<- '{ "_id" : ObjectId( "539163d7bd350003" ), "login" : "vui", "id" : 369607,

How to use stringr's replace_all() function to replace specific matches in a string

落爺英雄遲暮 提交于 2019-12-23 10:51:54
问题 The stringr package has helpful str_replace() and str_replace_all() functions. For example mystring <- "one fish two fish red fish blue fish" str_replace(mystring, "fish", "dog") # replaces the first occurrence str_replace_all(mystring, "fish", "dog") # replaces all occurrences Awesome. But how do you Replace the 2nd occurrence of "fish"? Replace the last occurrence of "fish"? Replace the 2nd to last occurrence of "fish"? 回答1: For the first and last, we can use stri_replace from stringi as it