Questions tagged [gsub]

Ruby, Lua, R and Awk methods for performing a global pattern substitution.

Ruby

The String#gsub method replaces all occurrences of a pattern (a regex or a plain string) with another string or the result of a block. gsub returns a copy of the original string with the replacements applied, gsub! performs that replacement in-place.

Lua

If used simply, it will replace all instances of provided with the replacement passed as argument. Instead, if the replacement is a function and not a string, the function receives all matched instances of the pattern as arguments. The string value; if returned, by the function is then substituted back in original string.

R

The function gsub replaces all occurrences of a pattern (a regex or a plain string) with another string or the result of a block. The function returns a new object; the original object is not modified. The function is vectorized and hence can be applied to vectors of strings too.

Awk

The function gsub(p, s [, t]) replaces all occurrences of a pattern p (a regex or a plain string) with another string s, either in the named target variable, t or in $0 if no target is given.

2445 questions
0
votes
2 answers

How to remove certain characters that don't belong to any characters of an other character vector?

I have a uncleaned character vector, and I want to remove certain characters in that vector that don't belong to another character vector. So basically I know what I want to keep, but I don't know exactly what to remove, which makes gsub() and…
Yaxin Dai
  • 41
  • 4
0
votes
2 answers

How do I split one-column data separated by spaces into multiple columns?

I need to split the column "leg1" into three columns (v1,v2,v3). The problem is that the amount of blank spaces is different by rows. > head(data[,2:3]) name leg1 513 1 0.00000 0.00000 0.00000 514 2 …
Kat
  • 47
  • 5
0
votes
1 answer

gsub() not working if I reference a column using a character vector?

NOTE: I am new to R and appreciate any and all help. We all start somewhere! I want to perform the following operation on a project aimed at increasing my R skills: preferences[, (cols) := gsub(cols, pattern = "UN1", replacement = "A")] cols is a…
AaronSzcz
  • 145
  • 1
  • 8
0
votes
2 answers

R Regex expression with gsub

I am using gsub regex to select last part of expression Example: "Bla-text-01" - I want -> "text-01" "Name-xpto-08" - I want -> "xpto-08" "text-text-04" - I want -> "text-04" "new-blaxpto-morexpto-07" - I want ->…
0
votes
2 answers

How to use gsub with array element in ruby

I am trying to remove some speicial character from array but it showing error undefined method gsub! for array def get_names Company.find(@company_id).asset_types.pluck(:name).reject(&:nil?).gsub!(/([^,-_]/, '').map(&:downcase) end
smith
  • 375
  • 2
  • 13
0
votes
1 answer

How to replace special characters with gsub in R?

I have a text that is written with the old version of romanian letters. Old New ş (s with a cedilla)UTF-8: c59f ș (s with a comma)UTF-8: c899 ţ (s with a cedilla)UTF-8: c5a3 ț (t with a comma)UTF-8: c89b When I export the text from R…
Stefan
  • 19
  • 4
0
votes
3 answers

r remove keywords in a column

I have a column in my dataframe with words like this. ColA 2-4 Model Group1 Group ACH Group2 Phenols Group1 Group ACH Group2 MONO MHPP Group1 Group ACH Group2 I want to create two additional columns like this: 1) without keywords c("Group1", "Group…
0
votes
3 answers

Improving the readability of automated text generation based on a database query

I am trying to improve the readability of automated text generation based on a database query. is there a neat way to perform these substitutions ? To do the following in 1 command instead of 6? x<-c("Te( )st", "Test()", "Test ()", "Test ( )", "Test…
ECII
  • 10,297
  • 18
  • 80
  • 121
0
votes
1 answer

Using an anonymous function within a function in R

I have here a list of genetic loci containing their alleles encoded as three digit numbers, as class character. I have a few lines of code to go through the list and convert all instances to nucleic base letters (ie. A, C, G, T). my_allele_list =…
rainbird
  • 193
  • 1
  • 9
0
votes
1 answer

How do I stop gsub from repeating? [Roblox Studio]

I have a string, which has the word "amogus" in it. I use gsub, to replace amogus with "".."amogus".."" But everytime I refresh to check for anything else I need to gsub, it thinks that the replaced text needs to be…
0
votes
2 answers

How to change values before text in string using R

I have multiple strings that are similar to the following pattern: dat<-("00000000AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0") I need to change all 0 values to "." before the first character value within a string. My desired output in this…
0
votes
3 answers

getting the last 10 words from a string, applied on a vector of strings

I have a vector of texts within a dataframe (df1$text), and I am trying to create a new vector with the last 10 words of the text (df1$last.ten). I've tried the following without success: df1$last.ten = mapply(function(x,y) paste(word(x,y),…
user17661126
  • 129
  • 5
0
votes
1 answer

Remove text from string

I'm trying to remove the text " 12:00:00 AM" from the string e.g: "4/12/2016 12:00:00 AM" from the column "ActivityDate" I'm using this pipe: Sleep <- sleepDay_merged %>% rename('ActivityDate' = 'SleepDay') %>% gsub("12:00:00…
0
votes
0 answers

gsub in FluentD and S3 not substituting unicode characters

Message appears as this string in S3 bucket: '\u00001\u00001\u0000/\u00002\u00003\u0000/\u00002\u00000\u00002\u00001\u0000…
0
votes
3 answers

How to conflate consecutive gsubs in ruby

I have the following address.gsub(/^\d*/, "").gsub(/\d*-?\d*$/, "").gsub(/\# ?\d*/,"") Can this be done in one gsub? I would like to pass a list of patterns rather then just one pattern - they are all being replaced by the same thing.
sanon
  • 6,403
  • 2
  • 21
  • 26
1 2 3
99
100