Questions tagged [gsub]

1

votes
2

answer
47

Views

R Returning all characters after the first underscore

Sample DATA x=c('AG.av08_binloop_v6','TL.av1_binloopv2') Sample ATTEMPT y=gsub('.*_','',x) Sample DESIRED WANT=c('binloop_v6','binloopv2') Basically I aim to extract all the characters AFTER the first underscore value.
bvowe
1

votes
1

answer
79

Views

Ruby case expression unexpected keyword

I am new to Ruby. My past is in Java. I am trying to use a switch case, apparently known as a case expression in Ruby. I want to accept user input, check that input to see if it includes certain characters, and then substitute those characters with other characters. When I try to run this simple pr...
Blank Reaver
1

votes
2

answer
45

Views

Misunderstanding sub function

I'm working on a Shiny app that loops through an html file replacing an instance of a phrase with a different phrase relative to its position. That is, the first time 'aa' comes, I put 'bluh', the second time 'aa' comes, I put 'gfgf'. I have a table of all the 2nd phrases in order. I think I'm mi...
Carlos M.
1

votes
3

answer
56

Views

gsub to extract text containing spaces [closed]

I am working with a large (32666 rows) dataset in which most of the information is contained in an XML formatted cell. The XML pertains to approximately 50 fields that I need to individually extract for further analysis. I can accurately extract the information needed if I first eliminate all spaces...
regents
1

votes
1

answer
36

Views

Splitting values with no delimiter and uneven lengths

I scraped the second HTML table from the following site (http://www.floridahealth.gov/programs-and-services/office-of-medical-marijuana-use/medical-marijuana-treatment-centers/index.html) Below is my code to extract the table: FLlist % .[[2]] %>% html_table(header = TRUE)) write.csv(FLtest, 'FLTest...
Will Holt
1

votes
1

answer
38

Views

Confusion on newline and space match in R

I am confused with the code below and hope someone can give me insight, in which it seems that new line can be matched by space in gsub in R. Briefly, I used sprintf to prepare the SQL query statement, and to make the code readable and easy to modify, I splited the statement to mutliple lines. gsu...
Psytky
1

votes
0

answer
29

Views

How to make gsub() work on entire column?

I am trying to make gsub replace hex characters I have into Hebrew abc, Using the following function: name
DanBoguslavsky
1

votes
1

answer
68

Views

R shiny app error: unexpected string constant in gsub function for curly quotes

I'm building shiny app that does a lot of text mining. When I execute the function below in R to replace curly quotations “” and ‘’ by ' and ' respectively, it works beautifully. Example: essay
Yeshyyy
1

votes
2

answer
55

Views
1

votes
1

answer
52

Views

How can I remove all characters between two other recurring characters in a string using R?

The following code successfully gets me the text I need before using gsub to help 'clean.' am1
sebas
1

votes
2

answer
55

Views

How to replace a pattern except specific pattern with another pattern in a column?

I have a data frame(x) with 'Symbol' column, which I want to replace (all '-*' with '') but I don't want change some values such as: 1-Mar, 1-Sep, 1-Dec,... x
Morteza Razavi
1

votes
1

answer
31

Views

Replace special character with lapply and gsub in rstudio

I am trying to clean up my data by applying the following codes: Manuf
Amy Chen
1

votes
2

answer
31

Views

How to remove part of a rowname after the second underscore

I want to get all the characters before the second underscore in a string. Any ideas how I can do this? I have this: Part1-Part2_Part3_Part4 I want this: Part1-Part2_Part3 all the parts can vary from one another and there can also be more than 4 parts.
Kerstin Thol
1

votes
1

answer
15

Views

Remove selected whitespaces from string in R

I am really stuck at some point by trying to get rid of some whitespaces in my character string that I obtained from a PDF. The string looks as follows: [1] ' ZUSAMMEN 8 756 3 193 1 456 531 506 1 931 1 780 225...
Niklas
0

votes
0

answer
9

Views

R - Only select last part of a string after the last point

I have a dataframe with one column that represents the requests made by my users. A few examples look like this: GET /enviro/html/tris/tris_overview.html GET /./enviro/gif/emcilogo.gif GET /docs/exposure/meta_exp.txt.html GET /hrmd/ GET /icons/circle_logo_small.gif I only want to select the last par...
nielsvg
1

votes
1

answer
252

Views

Rails 3 working with multiple languages

When working with different languages, what is the proper way to sub a string out in Rails? Example (Czech Translation): str = 'pro více informací' replace = 'více' str['více'] = replace puts str The problem I keep running into (and this is for multiple languages, not just Czech) is the followin...
dennismonsewicz
1

votes
1

answer
337

Views

How can i use gsub to replace “0” (only)

gsub('$0\n','') isn't working I would prefer something similar. I want: (note the 10 and 20 have to work with 0 not being replaced in them). If I have: 23 12 0 15 9 0 10 20 0 I want: 23 12 15 9 10 20
1

votes
2

answer
1.2k

Views

html_safe and gsub

In my CMS application I use gsub to replace parts of a user supplied html template (string) with dynamic code from the application (such as a comment from a user, or page content). I'm upgrading from Rails 2.3.5 to 2.3.12 in preparation for a move to Rails 3 and have installed the rails_xss gem to t...
Brendon Muir
1

votes
1

answer
324

Views

Braces in regexp for ruby sub-method block

I have a string: s = 'bbb' I want to get aaa and bbb in ruby block for sub method. If I call: s.sub(/([\s\S]+)/,'first=\\1 second=\\2') everything works as I expect, so the output is 'first=aaa second=bbb'. Then I call the same regexp with ruby-block, but it returns only the whole string but not \\1...
Kasheftin
1

votes
2

answer
149

Views

using variables in gsub

I have a variable address which for now is a long string containing some unneccessary info, eg: 'Aboriginal Relations 11th Floor Commerce Place 10155 102 Street Edmonton AB T5J 4G8 Phone 780 427-9658 Fax 780 644-4939 Email [email protected]' Aboriginal Relations is in a variable called title,...
agarrow
1

votes
1

answer
779

Views

Replacing numbers R regular-expression

I am trying to code up an html tagging tool for my code in R and I am having difficulty finding and replace numbers with colored numbers. I think the following is in the right direction but I am not sure what to do: txt
fsmart
1

votes
1

answer
1.5k

Views

gsub command to substitute a word starting with a specific letter in R

My question is what is the gsub command to substitute for a word starting with a specific letter. My main goal is to remove all URL's from a given text. For example, I have a text: 'refer http://www.google.com for further details'. What I need to do is, transform the text to 'refer for further detai...
N2M
1

votes
1

answer
230

Views

How replacing with regex characters generated by encoding errors when embedded in text

I need to replace the following characters with regex (gsub): ÃÆè -> è ÃÆà-> à ÃÆò -> ò ÃÆì -> ì ÃÆÃù -> ù My strategy is to first remove the first three characters ÃÆà that are common to all and the move to the last, leaving à at the end since it is basic...
CptNemo
1

votes
2

answer
178

Views

how to remove part of a string (not just replace it) via regex ruby

I have a string from a api response that I use a after_filter to adjust the body of the message. I am replacing a few characters and removing a whole node. response.body = response.body.gsub(/\/, '').gsub( /\/, '').gsub(/\/, '') This somewhat works. Except as expected the part that I want to delete...
user2334188
1

votes
1

answer
743

Views

gsub with grepl together

I want to extract part of a string but with an if condition. So that I dont extract anything if this if condition is true. my code sofar u
user2963882
1

votes
2

answer
112

Views

How to write replace function more elegant?

I wrote some function which replace one value in a string. It take me some time to figure-out it basing on varius docs, but I would like to ask some champions in RUBY about RIGHT WAY, how to code this? def replace(newValue) =begin [0]: A0000001561234 = 10,10,1,X,0,0,12345,12345 1. A0000001561234 =...
Artur_PL
1

votes
1

answer
156

Views

r gsub and regex, obating y*_x* from y*_x*_xxxx.csv

General situation: I am currently trying to name dataframes inside a list in accordance to the csv files they have been retrieved from, I found that using gsub and regex is the way to go. Unfortunately, I can’t produce exactly what I need, just sort of. I would be very grateful for some hints...
user3275209
1

votes
2

answer
1.6k

Views

Why will this .each_char method not replace the character specified?

This is a straightforward and simple question. In the code below, this should replace the character specified with an underscore, but it isn't. I've looked at the documentation for both .each_char and .gsub and this combination should work. Why does this snippet of code not work? string = 'string' n...
Dan Rubio
1

votes
2

answer
48

Views

Deleting lots of characters from a list in R

I have a list of characters with sentences. I have about 10000+ lines. I want to delete 1000+ words from it. So I have a character vector with the words to be deleted. I am using the approach as follows: c
0

votes
1

answer
24

Views

How to replace the missing values with the median for the variable using gsub in R?

I have a data frame that I extracted from an html file of a Wikipedia page table. I want to replace the missing values with the median of each variable. From the hints given I know that I need to convert the factor type to numeric values, and I likely need to use as.numeric(gsub()). renew$Hydro[gr...
Laurel Daly
1

votes
1

answer
78

Views

gsub a text with code

In Lua I have the following text and want for format it: Incoming: Example Example 2 Example 3 Expected result: Example Example 2 Example 3 The values can be different (e.g. Example, This is a Text...). How to this? Do I have to use gsub with a pattern?
frgtv10
1

votes
2

answer
146

Views

£ considered invalid character

I need to search for a '£' sign in my text but it keeps coming up with the error: invalid character property name {`£`}: /\p{`\u00A3`}/ (SyntaxError) i have # encoding: utf-8 at the top, the context in which I'm using it is: original_contents
Rebs
1

votes
3

answer
782

Views

How to replace dot in linux

I Have a file with a column as shown below: 1000g2012apr_all 0.07 0.17 0.16 0.23 0.04 . 0.23 0.15 0.24 Using the solution, awk 'BEGIN {OFS=FS='\t'} {gsub(/\./,'',$1)}1' replaces all the dots in the first column. But i would like to replace only those without any value. The output should look like b...
chas
1

votes
4

answer
961

Views

Ruby: sub/gsub at a particular line OR before/after a pattern

I know that I can replace text as below in a file File.write(file, File.read(file).gsub(/text/, 'text_to_replace')) Can we also use sub/gsub to:- Replace a string on a particular line number (useful when there is a same string at different locations in a file) Example [email protected]:~# cat file.txt fix...
vikas027
1

votes
1

answer
1.6k

Views

Remove hyphen at the end of string in R

I have a column of a dataframe in R like: names
Optimus
1

votes
1

answer
619

Views

R: gsub with fixed=T or F and special cases

Building on top of two questions I previously asked: R: How to prevent memory overflow when using mgsub in vector mode? gsub speed vs pattern length I do like suggestions on usage of fixed=TRUE by @Tyler as it speeds up calculations significantly. However, it's not always applicable. I need to subst...
Alexey Ferapontov
1

votes
1

answer
1.2k

Views

Removing unwanted comma and updating a column

I have a dataset like this Id Comments 1 ,,,,A,,,,,B,,,,,,C 2 ,,,,,,A,,,,,,,C,,, 3 4 ,,,,,,,,,,Z,,,,,,,, What I am trying to produce is an output like this Id Feedback Comments 1 Yes A,B,C 2 Yes A,C 3 NA 4 Yes...
Kim Jenkins
1

votes
1

answer
139

Views

Parse a long string using gsub and replacement is reserved char in R

I have a character vector of the form (below). When you read this vector it is one long string such as, 'tBodyAcc-XYZ\tGravityAcc-XYZ\tBodyAccJerk-XYZ\BodyGyro-XYZ...' where whitespace is split by the escape char '\'. How can I use gsub to replace the white space pattern = ' ', replacement = ' ', so...
Zach
1

votes
2

answer
536

Views

Ruby Regex Replace Last Occurence of Grouping

In Ruby regular expressions I would like to use gsub to replace a last occurrence of a grouping, if it occurs, otherwise, perform a replacement anyways at a default location. I am trying to replace the last occurrence of a number in the 40s (40...49). I have the following regular expression, which i...
fowlball1010
1

votes
2

answer
354

Views

Truncate words within each element of a character vector in R

I have a data frame where one column is a character vector and every element in the vector is the full text of a document. I want to truncate words in each element so that maximum word length is 5 characters. For example: a
Jim

View additional questions