Remove special word using gsub

Advertisements I try to clean some text and I would like to remove the following text from a string googletag.cmd.push(function() { googletag.display(‘div-gpt-ad-1513202928332-3’); }); For example, if x="123 googletag.cmd.push(function() { googletag.display(‘div-gpt-ad-1513202928332-3’); }); 456" then gsub("googletag.cmd.push(function() { googletag.display(‘div-gpt-ad-1513202928332-3’); });, ", x) The desired output is [1] 123456 Thank you >Solution : Regex approach You can use the… Read More Remove special word using gsub

Gsubing when we have multiple backslashes and/special characters

Advertisements I have a string in which I want to get out the city, in this example it would be ‘Elland Rd’ and ‘Leeds’. mystring = "0000\" club_info=\"Elland Rd, Leeds\" Pitch=\"100×50\"" city = gsub(".* club_info=\"(.*),(.+)\.*", "\\2", mystring) #cant get this part to work My theory behind getting the city is to search for everything after… Read More Gsubing when we have multiple backslashes and/special characters

Append characters to strings in column which match expression R

Advertisements I have a date/time column in an inconsistent format: df <- data.frame(date.time=c("10-29-2022 09:46:40", "02-27-2023 22:53:53", "12-15-2022 02:54:03", "01-09-2023 14:44", "03-05-2023 14:58", "02-18-2023 19:46:35", "12-10-2022 16:50")) I want to add ":00" to the time stamps that don’t include seconds (notice some strings are shorter than others). I can find the strings without seconds using gsubs(pattern=)… Read More Append characters to strings in column which match expression R

gsub catches only last character of group

Advertisements I have this character vector vec <- c("(0,13.2]", "(13.2,28.3]", "(28.3,39.3]", "(39.3,49.4]", "(49.4,59.4]", "(59.4,69.3]", "(69.3,78.9]", "(78.9,87.8]", "(87.8,95.5]", "(95.5,100]") and I want to change the entries to expected <- c("0 to 13.2", "13.2 to 28.3", "28.3 to 39.3", "39.3 to 49.4", "49.4 to 59.4", "59.4 to 69.3", "69.3 to 78.9", "78.9 to 87.8", "87.8 to 95.5",… Read More gsub catches only last character of group

Extract everything before second delimiter in R

Advertisements Building off this previous post: How to extract string after 2nd delimiter in R Have a string like the following: "dat1/set1/set1_covars.csv" And want to extract all the values before the second / like: "dat1/set1/" I was using variations of: sub("^([^/]+/){2}", "", "dat1/set1/set1_covars.csv") With ^ and .* moved around in different places, but just can’t… Read More Extract everything before second delimiter in R