gsub

Awk gsub and the mysterious “1”

萝らか妹 提交于 2019-12-14 04:19:25
问题 I think it is a more general understanding problem but here is my question: If I run the following command in the terminal: awk '{gsub("a","H")}1' on the file marks.txt: 1) Amit Physics 80 2) Rahul Maths 90 3) Shyam Biology 87 4) Kedar English 85 5) Hari History 89 I get the following result: 1) Amit Physics 80 2) RHhul MHths 90 3) ShyHm Biology 87 4) KedHr English 85 5) HHri History 89 But if I run it without that "1", I get nothing. awk '{gsub("a","H")}' Why is this one so important and

Convert time values to numeric while keeping time characteristics

筅森魡賤 提交于 2019-12-14 03:57:41
问题 I have a data set which contains interval times of different events occurring. What I want to do, is convert the data into a numeric vector, so its easier to manipulate and run summaries/make graphs etc, while keeping its time characteristics. Here is a snippet of my data: data <- c( "03:31", "12:17", "16:29", "09:52", "04:01", "09:00", "06:29", "04:17", "04:42") class(data) [1] character The obvious answer is : as.numeric(data) But I get this error: Warning message: NAs introduced by

input.gsub(numbers) { |m| p $~ } Matching data in Ruby for all occurrences in a string

安稳与你 提交于 2019-12-14 03:04:28
问题 Answer from How do I get the match data for all occurrences of a Ruby regular expression in a string?: input = "abc12def34ghijklmno567pqrs" numbers = /\d+/ input.gsub(numbers) { |m| p $~ } Result is as requested: ⇒ #<MatchData "12"> ⇒ #<MatchData "34"> ⇒ #<MatchData "567"> Would someone break down what the answerer is doing in input.gsub(numbers) { |m| p $~ } ? Also, how would I access each of the MatchData s? 回答1: Since I’m the answerer, I would try to explain. $~ is one of Ruby predefined

Any advice on using gsub? Works fine in irb console, but won't do find/replace in actual files

不打扰是莪最后的温柔 提交于 2019-12-14 03:03:56
问题 I’ve got some a basic framework for a webapp (some static pages, user authentication, basic testing with rpec). I’d like to use this as a foundation for future webapps. I’ve named my project “framework,” so if I fork it, I’ll have about a dozen instances where I have to replace the term “Framework” with the name of the new app I’m creating. This project is as github.com/bnd5k/framework. The README file contains a list of the files where I need to make the change. I can make those changes by

Regex to remove white space between tags in gsub R

断了今生、忘了曾经 提交于 2019-12-13 15:26:52
问题 How to remove white space or tabulation between tags, without removing it from inside the tags, i tried gsub but didn't succeed gsub("(^>)\\s(^<)", "", x) Given a string like : "<div class=\"panel\">\n <div class=\"shortcode\">\n\t <div class=\"article-\"> text text text text </div> \n </div>\n </div>" Desired output: <div class=\"panel\"><div class=\"shortcode\"><div class=\"article-\"> text text text text </div></div></div> 回答1: You could try using a look around gsub("(?<=\\>)(\\s*)(?=\\<)"

Formatting string with RegExp to set delimiter

蹲街弑〆低调 提交于 2019-12-13 14:37:21
问题 I'm trying to format a string as follows Ensure all of numbers use dashes for delimiters. Example: 480.01.4430 and 480014430 would both be 480-01-4430. this is what I have come up with so far, but I can't understand why it doesn't work def format_ssns(string) ssn = string[/\d{9}/] ssn.gsub(/\d{9}/, /\d{3}-\d{2}-\d{4}/) end 回答1: It's strange that you're not getting an exception: The second argument to gsub is required to be a String (or something that can be converted to a String), not a

R: Replace “+” character with gsub

别等时光非礼了梦想. 提交于 2019-12-13 13:26:50
问题 the question seems totally trivial but I cannot figure out why it isn't working. I simply want to replace a character variable involving a "+" operator with a single value excluding the "+" operator. For some reason gsub() and sub() function replace the number value but keep the operator. Any hint on how this can be overcome? Many thanks! data <- c(1,2,3,4,"5+") gsub(pattern="5+",replacement="5",x=data) #[1] "1" "2" "3" "4" "5+" gsub(pattern="5+",replacement="",x=data) #[1] "1" "2" "3" "4" "+

Replacing row elements in a dataframe based on values from another dataframe [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-13 09:16:54
问题 This question already has answers here : How to join (merge) data frames (inner, outer, left, right) (13 answers) Closed 9 months ago . I'm fairly new to R so I hope somebody can help me. An output table in one of my scripts is the averagetable below showing different proportions of the event Standing in three different clusters: > print(averagetable) Group.1 Standing 1 cluster1 0.5642857 2 cluster2 0.7795848 3 cluster3 0.7922980 Note that R can assign different cluster names ( cluster1 ,

How do I replace values in a matrix from an uploaded CSV file in R?

情到浓时终转凉″ 提交于 2019-12-13 03:39:26
问题 These are the steps I took: 1) Read in CSV file rawdata <- read.csv('name of my file', stringsAsFactors=FALSE) 2) Cleaned my data by removing certain records based on x-criteria data <- rawdata[!(rawdata$YOURID==""), all()] data <- data[(data$thiscolumn=="right"), all()] data <- data[(data$thatcolumn=="right"), all()] 3) Now I want to replace certain values throughout the whole matrix with a number (replace a string with a number value). I have tried the following commands and nothing works

Replace categorical values with traffic light colors

人盡茶涼 提交于 2019-12-12 22:44:50
问题 I have some categorical values I want to show in a table as red, yellow and green spots in R or r-markdown. Is there a way to do this? maybe an icon package that will let me gsub text for an Icon? I have scoured the internet and come up empty handed. Any thoughts are appreciated. library(kableExtra) dt <- mtcars[1:5, 1:6] kable(dt) %>% kable_styling(full_width = F, font_size = 12) %>% row_spec(0, angle = -90)%>% row_spec(1:5, bold = F, color = "black") 回答1: Here's an example that uses html