logo
down
shadow

Issue with replacing string by match in R


Issue with replacing string by match in R

By : user2173718
Date : October 20 2020, 08:10 AM
Hope this helps I want to replace the cell elements to "NA" if a cell element starts with "F" and all trailing cell elements in a row. , I started by truncating your data for brevity:
code :
x <- x[,5:12]
 t(apply(x, 1, function(a) grepl("^F", a)))
#       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]
# [1,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
# [2,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
# [3,] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE
# [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
# [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
# [6,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
t(apply(x, 1, function(a) cumany(grepl("^F", a))))
#       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7] [,8]
# [1,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE TRUE
# [2,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE TRUE
# [3,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE TRUE
# [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
# [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
# [6,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE TRUE
x[ t(apply(x, 1, function(a) cumany(grepl("^F", a)))) ] <- NA
x
#    V33  V34  V35  V36  V37  V38  V39  V40
# 1: 013 <NA> <NA> <NA> <NA> <NA> <NA> <NA>
# 2: 005 <NA> <NA> <NA> <NA> <NA> <NA> <NA>
# 3: 035 <NA> <NA> <NA> <NA> <NA> <NA> <NA>
# 4: 001 L002  044    N  004    E  036 <NA>
# 5: 001 L002  044    N  004    E  036 <NA>
# 6: 005 <NA> <NA> <NA> <NA> <NA> <NA> <NA>


Share : facebook icon twitter icon
Replacing string after match

Replacing string after match


By : Inventor
Date : March 29 2020, 07:55 AM
it fixes the issue Assuming your date is always in that format, you can use a more general regular expression to replace the date:
code :
my $string = 'startDate="2014-06-10"';
$string =~ s/startDate="\d{4}-\d{1,2}-\d{1,2}"/startDate=""/g;
my $string = 'startDate="2014-06-10"';
$string =~ s/\d{4}-\d{1,2}-\d{1,2}//g;
replacing the 1st to nth match of the string. javascript

replacing the 1st to nth match of the string. javascript


By : 45H
Date : March 29 2020, 07:55 AM
Hope that helps The problem is I want to replace a certain string on its 1st occurence up to the nth occurence. where n can be any number. , I think simple loop can do the job:
code :
function replaceByOccurence(input, regex, replacement, nthOccurence) {
    for (i=0; i<nthOccurence; i++)
       input = input.replace(regex, replacement);
    return input;
}
var replaced = replaceByOccurence(str, /one-two/, 'one', 3);
function replaceByOccurence(input, regex, replacement, num) {
    i=0;
    return input.replace(regex, function($0) { return (i++<num)? replacement:$0; });
}
var replaced = replaceByOccurence(str, /one-two/g, 'one', 3);
//=> 73ghtwom2j2htwo2717datwo213
Replacing all match in a String

Replacing all match in a String


By : Chris Dingyi Lin
Date : March 29 2020, 07:55 AM
should help you out You need to use the flags parameter of RegExp. Use a g to make the search global throughout the string. Try this:
code :
actual_string = actual_string.replace(new RegExp('@' + dynamic[i] + "@", 'g'), value);
Replacing character in string which does not match

Replacing character in string which does not match


By : user6653967
Date : March 29 2020, 07:55 AM
it should still fix some issue I have an input text box with a max length of 20. In this text box I only want to allow the following characters a-z, A-Z, 0-9, a whitespace, an underscore _, a dash -, a forward slash /, and a back slash \. , Instead of , use \s for space in regex. will kill your regex
code :
$("#se_del").on('keyup',function (e) {
    const regex = /[^a-zA-Z0-9\s_\/\-\\]+/g;
    if (regex.test(this.value)) {
        this.value = this.value.replace(regex, '');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="se_del" />
re.sub not replacing if the whole string doesn't match

re.sub not replacing if the whole string doesn't match


By : nuriveben
Date : March 29 2020, 07:55 AM
With these it helps The reason is your ^ and $ tokens. Those refer to the start and end of the string respectively.
re.sub(r'\d{4}', '', year_with_txt) works.
Related Posts Related Posts :
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • ROC curve from train/test set in caret R package
  • Random Forest for a mixture of categorical,numeric and "unwanted" variables which include missing values
  • extract certain data from multiple excel files with R
  • Matrix with counts of wins and losses between methods in R
  • Grouping string variables from a dataframe by best string match to make subsets
  • Reorder does not work after adding second geom_points
  • cover POS data formate to the one can apply Arules (Apriori)
  • Matching values between data frames based on overlapping dates
  • Grouped bar chart turns into stacked bar chart ggplot
  • R: How to fill in NA Values within a Column based on grouping?
  • Two action buttons, but only the first one, that is written in the server file, works?
  • Barchart grouped by variable both count up to 100 percent
  • Converting time in R to 24 hours
  • R - Web scrapping and downloading multiple zip files and save the files without overwriting
  • Find month and year inside string
  • Append multiple csv files into one file using R
  • Use `purrr::map` with k-means
  • R - 'data' is not an exported object from 'namespace:my_package'
  • Sum vector with number by dinamic intervals without looping
  • Issues with ave function in R: error "cannot allocate vector of size 419 kb."
  • Shiny system call with continuous updates
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org