stringr

str_extract_all: return all patterns found in string concatenated as vector

安稳与你 提交于 2020-02-24 12:09:29
问题 I want to extract everything but a pattern and return this concetenated in a string. I tried to combine str_extract_all together with sapply and cat x = c("a_1","a_20","a_40","a_30","a_28") data <- tibble(age = x) # extracting just the first pattern is easy data %>% mutate(age_new = str_extract(age,"[^a_]")) # combining str_extract_all and sapply doesnt work data %>% mutate(age_new = sapply(str_extract_all(x,"[^a_]"),function(x) cat(x,sep=""))) class(str_extract_all(x,"[^a_]")) sapply(str

str_extract_all: return all patterns found in string concatenated as vector

笑着哭i 提交于 2020-02-24 12:08:06
问题 I want to extract everything but a pattern and return this concetenated in a string. I tried to combine str_extract_all together with sapply and cat x = c("a_1","a_20","a_40","a_30","a_28") data <- tibble(age = x) # extracting just the first pattern is easy data %>% mutate(age_new = str_extract(age,"[^a_]")) # combining str_extract_all and sapply doesnt work data %>% mutate(age_new = sapply(str_extract_all(x,"[^a_]"),function(x) cat(x,sep=""))) class(str_extract_all(x,"[^a_]")) sapply(str

str_extract_all: return all patterns found in string concatenated as vector

六月ゝ 毕业季﹏ 提交于 2020-02-24 12:07:05
问题 I want to extract everything but a pattern and return this concetenated in a string. I tried to combine str_extract_all together with sapply and cat x = c("a_1","a_20","a_40","a_30","a_28") data <- tibble(age = x) # extracting just the first pattern is easy data %>% mutate(age_new = str_extract(age,"[^a_]")) # combining str_extract_all and sapply doesnt work data %>% mutate(age_new = sapply(str_extract_all(x,"[^a_]"),function(x) cat(x,sep=""))) class(str_extract_all(x,"[^a_]")) sapply(str

str_replace_all replacing named vector elements iteratively not all at once

若如初见. 提交于 2020-02-24 05:53:26
问题 Let's say I have a long character string: pneumonoultramicroscopicsilicovolcanoconiosis. I'd like to use stringr::str_replace_all to replace certain letters with others. According to the documentation, str_replace_all can take a named vector and replaces the name with the value. That works fine for 1 replacement, but for multiple it seems to do it iteratively, so the result is a replacement of the prelast iteration. I'm not sure this is the intended behaviour. library(tidyverse) text_string =

Using stringr to extract one or multiple words from text string in R

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-30 06:34:17
问题 I have the following data frame: df <- data.frame(city=c("in London", "in Manchester city", "in Sao Paolo")) I am using str_extract and return the word after 'in' in a separate column. library(stringr) str_extract(df$city, '(?<=in\\s)\\w+') This works fine for me in 95% of cases. However, there are cases like "Sao Paolo" above where my regex would return "Sao" rather than the city name. Can someone please help me with amending it to capture either: 1) everything to the end of the text string

Using stringr to extract one or multiple words from text string in R

假装没事ソ 提交于 2020-01-30 06:34:11
问题 I have the following data frame: df <- data.frame(city=c("in London", "in Manchester city", "in Sao Paolo")) I am using str_extract and return the word after 'in' in a separate column. library(stringr) str_extract(df$city, '(?<=in\\s)\\w+') This works fine for me in 95% of cases. However, there are cases like "Sao Paolo" above where my regex would return "Sao" rather than the city name. Can someone please help me with amending it to capture either: 1) everything to the end of the text string

Converting coordinates from degree with unconventional format to decimal degree

我怕爱的太早我们不能终老 提交于 2020-01-24 22:13:08
问题 I am trying to convert my data so that it can be plotting on a map. For example the data looks like: # A tibble: 2 x 2 Latitud Longitud <chr> <chr> 1 10º 35' 28.98'' N 3º 41' 33.91'' O 2 10º 35' 12.63'' N 3º 45' 46.22'' O I am trying to mutate it using the following: df %>% mutate( Latitud = str_replace_all(Latitud, "''", ""), lat_edit = sp::char2dms(Latitud), "°") Which returns and error: Error in if (any(abs(object@deg) > 90)) return("abs(degree) > 90") : missing value where TRUE/FALSE

How do I write a function to convert a character vector into a character vector of unique pairs of its elements?

被刻印的时光 ゝ 提交于 2020-01-24 19:28:08
问题 What the input would be: c("a", "b", "c") [1] "a" "b" "c" I want a function that returns: [1] "a;b" "a;c" "b;c" I need this function to work solely off its inputs. I've tried some stuff with purrr::map() and purrr::reduce() , but I haven't managed to get anything useful. 回答1: We can use combn from base R with FUN argument as paste combn(x, 2, FUN = paste, collapse = ";") #[1] "a;b" "a;c" "b;c" data x <- c("a", "b", "c") 回答2: Not the exact result but maybe it might be useful: test<-c("a", "b",

best way to manipulate strings in big data.table

痞子三分冷 提交于 2020-01-23 09:19:25
问题 I have a 67MM row data.table with people names and surname separated by spaces. I just need to create a new column for each word. Here is an small subset of the data: n <- structure(list(Subscription_Id = c("13.855.231.846.091.000", "11.156.048.529.090.800", "24.940.584.090.830", "242.753.039.111.124", "27.843.782.090.830", "13.773.513.145.090.800", "25.691.374.090.830", "12.236.174.155.090.900", "252.027.904.121.210", "11.136.991.054.110.100" ), Account_Desc = c("AGUAYO CARLA", "LEIVA

Difference between `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`, `stri_paste`?

不想你离开。 提交于 2020-01-20 07:02:25
问题 What are the differences between all of these functions that seem very similar ? 回答1: stri_join , stri_c , and stri_paste come from package stringi and are pure aliases str_c comes from stringr and is just stringi::stri_join with a parameter ignore_null hardcoded to TRUE while stringi::stri_join has it set to FALSE by default. stringr::str_join is a deprecated alias for str_c see: library(stringi) identical(stri_join, stri_c) # [1] TRUE identical(stri_join, stri_paste) # [1] TRUE library