stringr

stringr, str_extract: how to do positive lookbehind?

喜你入骨 提交于 2020-12-02 08:27:59
问题 Very simple problem. I just need to capture some strings using a regex positive lookbehind, but I don't see a way to do it. Here's an example, suppose I have some strings: library(stringr) myStrings <- c("MFG: acme", "something else", "MFG: initech") I want to extract the words which are prefixed with "MFG:" > result_1 <- str_extract(myStrings,"MFG\\s*:\\s*\\w+") > > result_1 [1] "MFG: acme" NA "MFG: initech" That almost does it, but I don't want to include the "MFG:" part, so that's what a

stringr, str_extract: how to do positive lookbehind?

眉间皱痕 提交于 2020-12-02 08:23:02
问题 Very simple problem. I just need to capture some strings using a regex positive lookbehind, but I don't see a way to do it. Here's an example, suppose I have some strings: library(stringr) myStrings <- c("MFG: acme", "something else", "MFG: initech") I want to extract the words which are prefixed with "MFG:" > result_1 <- str_extract(myStrings,"MFG\\s*:\\s*\\w+") > > result_1 [1] "MFG: acme" NA "MFG: initech" That almost does it, but I don't want to include the "MFG:" part, so that's what a

stringr, str_extract: how to do positive lookbehind?

不想你离开。 提交于 2020-12-02 08:22:10
问题 Very simple problem. I just need to capture some strings using a regex positive lookbehind, but I don't see a way to do it. Here's an example, suppose I have some strings: library(stringr) myStrings <- c("MFG: acme", "something else", "MFG: initech") I want to extract the words which are prefixed with "MFG:" > result_1 <- str_extract(myStrings,"MFG\\s*:\\s*\\w+") > > result_1 [1] "MFG: acme" NA "MFG: initech" That almost does it, but I don't want to include the "MFG:" part, so that's what a

Replace part of string with mutate (in a pipe)

旧街凉风 提交于 2020-08-27 07:52:31
问题 I would like to replace a part of a string (between the first 2 underscores, the first group always being "i") like in the base R example below: library(dplyr) library(stringr) d <- tibble(txt = c("i_0000_GES", "i_0000_OISO", "i_0000_ASE1333"), repl = c("1111", "1111", "2222")) str_sub(d$txt, 3, 6) <- d$repl d # A tibble: 3 x 2 # txt repl # <chr> <chr> # 1 i_1111_GES 1111 # 2 i_1111_OISO 1111 # 3 i_2222_ASE1333 2222 How can I do that either using str_sub<- or another stringr-function? 回答1:

extracting city and state information from a google street address

China☆狼群 提交于 2020-08-25 06:56:38
问题 I have a data set that contained lat/long information for different point locations, and I would like to know which city and state are associated with each point. Following this example I used the revgeocode function from ggmap to obtain a street address for each location, producing the data frame below: df <- structure(list(PointID = c(1787L, 2805L, 3025L, 3027L, 3028L, 3029L, 3030L, 3031L, 3033L), Latitude = c(38.36648102, 36.19548585, 43.419774, 43.437222, 43.454722, 43.452643, 43.411949,