stringr

extracting city and state information from a google street address

岁酱吖の 提交于 2020-08-25 06:56:07
问题 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,

R, stringr - replace multiple characters from all elements of a vector with a single command

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-07 07:55:52
问题 First, I am new to R and programming in general, so I apologise if this turns out to be a stupid question. I have a character vector similar to this: > vec <- rep(c("XabcYdef", "XghiYjkl"), each = 3) > vec [1] "XabcYdef" "XabcYdef" "XabcYdef" "XghiYjkl" "XghiYjkl" "XghiYjkl" Using the stringr package, I would like to drop the leading "X" and replace the "Y" with a "-". I have tried the following code, but the result is not what I was hoping for. It looks like the pattern and replacement

Is there a way to create an R function using a string formula with ', and “ =~”?

一曲冷凌霜 提交于 2020-07-19 18:07:24
问题 I'm trying to create an R function that lets me specify latent variables and indicators. Is there a way to convert the following three code lines into a function? ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' I tried using paste and paste0 but it didn't work very well. For example, using just one latent variable, I tried this: myFunction <- function(z, x, ...) { latent_variable <- paste0(x) latent_indicators <- paste0(..., collapse = " + ") latent_formula <- paste0(

Why is R coercing ′ to ' when I try to assign it as a simple character vector but not when I extract it from a list object?

最后都变了- 提交于 2020-07-10 07:31:22
问题 Why is R coercing ′ (the prime symbol) to ' (apostrophe) when I try to assign it as a simple character vector but not when I assign it directly from an list/tibble? Unfortunately, I can't find a simple way to share the data which is the root of this problem, but please hear me out! It is entirely reproducible if you save a super simple .xlsx file with a single value entered (36°48′31.33): and hopefully it will become clear in a second why I can't share this in a more efficient manner. If I

Why is `str_extract` only catching some of these values?

血红的双手。 提交于 2020-07-07 09:56:29
问题 I have a table that has a "membership type" column that includes a zillion different membership levels that we've used over the years. example <-data.frame(membership = c( "Legacy Payment ID #3564, Payment Record #0, Period Paid: 1 Flag: N", "Legacy Payment ID #3611, Payment Record #0, Period Paid: 2 Flag: N", "Legacy Payment ID #4105, Payment Record #0, Period Paid: 1 Flag: G", "Legacy Payment ID #4136, Payment Record #0, Period Paid: 1 Flag: N", "Legacy Payment ID #5191, Payment Record #0,

Is there a way to replace ′ (prime) in a string using str_replace_all?

时光怂恿深爱的人放手 提交于 2020-06-17 09:11:52
问题 I'm trying to format various coordinates in degrees/minutes and degrees/minutes/seconds prior to passing through measurements::conv_unit(), which requires the input as numbers separated by spaces. I've read various answers to similar questions, such as this one: Remove all special characters from a string in R? Which lead me to initially try: library(tidyverse) latitude <- "-36°48′31.33" str_replace_all(string = latitude, pattern = c("°|'|\"|′|″"), repl = " ") However, the prime symbol (′) is

Is there a way to replace ′ (prime) in a string using str_replace_all?

心已入冬 提交于 2020-06-17 09:10:24
问题 I'm trying to format various coordinates in degrees/minutes and degrees/minutes/seconds prior to passing through measurements::conv_unit(), which requires the input as numbers separated by spaces. I've read various answers to similar questions, such as this one: Remove all special characters from a string in R? Which lead me to initially try: library(tidyverse) latitude <- "-36°48′31.33" str_replace_all(string = latitude, pattern = c("°|'|\"|′|″"), repl = " ") However, the prime symbol (′) is

Extracting a string of words from a string vector data

无人久伴 提交于 2020-05-29 09:50:06
问题 I have a string vector data as shown below Data Posted by Mohit Garg on May 7, 2016 Posted by Dr. Lokesh Garg on April 8, 2018 Posted by Lokesh.G.S on June 11, 2001 Posted by Mohit.G.S. on July 23, 2005 Posted by Dr.Mohit G Kumar Saha on August 2, 2019 I have used str_extract() function as str_extract(Data, "Posted by \\w+. \\w+ \\w+") It generated the output as [1] "Posted by Mohit Garg on" "Posted by Dr. Lokesh Garg" NA [4] NA NA I want the output should like [1] "Posted by Mohit Garg on"

Which regex removes punctuation from quotation marks in text

﹥>﹥吖頭↗ 提交于 2020-04-06 08:26:31
问题 I have a database and throughout the text there are some quotes that are in quotation marks. I would like to remove all the dots "." that are enclosed in quotation marks in the text. I have code that punctuates text in quotation marks but if there is more than one quote or more than one point, only the first one is removed. # Simple phrase: string <- '"é preciso olhar para o futuro. vou atuar" no front ' # Code that works for a simple 1-point sentence: str_replace_all(string, '(\".*)\\.(.*\")

str_extract: Extracting exactly nth word from a string

半城伤御伤魂 提交于 2020-03-14 18:59:17
问题 I know this question has been asked at several places, but I didnt see a precise answer to this. So I am trying to extract exactly the 2nd word from a string("trying to") in R with the help of regex. I do not want to use unlist(strsplit) sen= "I am trying to substring here something, but I am not able to" str_extract(sen, "trying to\\W*\\s+((?:\\S+\\s*){2})") Ideally I want to get "here" as an output, but I am getting "trying to substring here" 回答1: You may actually capture the word you need