gsub

parse values based on groups in R

五迷三道 提交于 2019-12-12 12:16:38
问题 I have a very large dataset and a sample of that looks something like the one below: | Id | Name | Start_Date | End_Date | |----|---------|------------|------------| | 10 | Mark | 4/2/1999 | 7/5/2018 | | 10 | | 1/1/2000 | 9/24/2018 | | 25 | | 5/3/1968 | 6/3/2000 | | 25 | | 6/6/2009 | 4/23/2010 | | 25 | Anthony | 2/20/2010 | 7/21/2016 | | 25 | | 9/12/2014 | 11/26/2019 | I need to parse the names from Name column based on their Id such that the output table looks like: | Id | Name | Start_Date

How to replace “unexpected escaped character” in R

我怕爱的太早我们不能终老 提交于 2019-12-12 11:10:17
问题 When I try to parse JSON from the character object from a Facebook URL I got "Error in fromJSON(data) : unexpected escaped character '\o' at pos 130". Check this out: library(RCurl) library(rjson) data <- getURL("https://graph.facebook.com/search?q=multishow&type=post&limit=1500", cainfo="cacert.perm") fbData <- fromJSON(data) Error in fromJSON(data) : unexpected escaped character '\o' at pos 130 #with RSONIO also error > fbData <- fromJSON(data) Erro em fromJSON(content, handler, default

How to replace second or more occurrences of a dot from a column name

孤街醉人 提交于 2019-12-12 10:00:33
问题 Folks, how can I replace second occurrence of a dot from column names? Sample data: age.range.abc = sample(c("ar2-15", "ar16-29", "ar30-44"), 200, replace = TRUE) gender.region.q = sample(c("M", "F"), 200, replace = TRUE) region_g.a = sample(c("A", "B", "C"), 200, replace = TRUE) physi = sample(c("Poor", "Average", "Good"), 200, replace = TRUE) survey = data.frame(age.range.abc, gender.region.q, region_g.a,physi) head(survey) I tried this but it removes all dots with underscore. I want to

How do I limit the number of replacements when using gsub?

浪子不回头ぞ 提交于 2019-12-12 08:48:38
问题 How do you limit the number of replacements made by String#gsub in Ruby? In PHP this can be easy done with preg_replace which takes a parameter for limiting replacements, but I can't figure out how to do this in Ruby. 回答1: gsub replaces all occurences. You can try String#sub http://ruby-doc.org/core/classes/String.html#M001185 回答2: You can create a counter and decrement that within a gsub loop. str = 'aaaaaaaaaa' count = 5 p str.gsub(/a/){if count.zero? then $& else count -= 1; 'x' end} # =>

Using gsub/regex to place quotes around names inside string

落爺英雄遲暮 提交于 2019-12-12 06:53:07
问题 I have tried searching, but cannot find the exact thing I am looking to do. My apologies if I have overlooked it. I am trying to take a long vector of character strings, all with the same general structure, and place them into a data.frame. The structure is as follows: [1] "rank, team, record" [2] "1 Team 22-4" [3] "2 Long Team Name 20-6" My initial thought was to use gsub and a regex expression to place /" around the team names (ex. /"Long Team Name/") then use read.table to import, but I am

Add backslash before a character in a string [duplicate]

妖精的绣舞 提交于 2019-12-12 05:31:11
问题 This question already has an answer here : R - gsub replacing backslashes (1 answer) Closed 2 years ago . I want to add the "\" character to my string using R. My string looks like this: q <- "U0E2BU0E25" I want to add a backslash before the letter "U" so the result would look like this: \U0E2B\U0E25 I have tried using gsub: gsub("U", "\U", q) but received an error: Error: '\U' used without hex digits in character string starting ""\U" 回答1: We need to escape the backslash. gsub("U", "\\\\U",

R: gsub and str_split_fixed in data.tables

落爺英雄遲暮 提交于 2019-12-12 04:36:31
问题 I am "converting" from data.frame to data.table I now have a data.table: library(data.table) DT = data.table(ID = c("ab_cd.de","ab_ci.de","fb_cd.de","xy_cd.de")) DT ID 1: ab_cd.de 2: ab_ci.de 3: fb_cd.de 4: xy_cd.de new_DT<- data.table(matrix(ncol = 2)) colnames(new_DT)<- c("test1", "test2") I would like to to first: delete ".de" after every entry and in the next step separate every entry by the underscore and save the output in two new columns. The final output should look like this: test1

Regular expression parsed with grepl replacement

孤街醉人 提交于 2019-12-12 02:25:46
问题 The objective is to parse a regular expression and replace the matched pattern. Consider this example: data <- c("cat 6kg","cat g250", "cat dog","cat 10 kg") I have to locate all occurrences of cat and a number [0-9] . To do this: found <- data[grepl("(^cat.[a-z][0-9])|(^cat.[0-9])",data)] found [1] "cat 6kg" "cat g250" "cat 10 kg" The next step is to replace each element of found with string cat . I have attempted gsub , sub , and gsubfn() from package (gsubfn) according to Stack question

How to use gsub regex in ruby

若如初见. 提交于 2019-12-12 01:52:44
问题 I want to remove some part of string from using ruby regex: value = localhost:8393/foobar/1 test:foobartest I want to remove "test" from my string [localhost:8393/foobar/1 test:foobartest] and rest of the value so that output should look like: localhost:8393/foobar/1 How to do this in ruby? Can you share some sample code to achieve this? Appreciated your help in advance! Thanks! 回答1: I would do something like this: value = 'localhost:8393/foobar/1 test:foobartest' value.split.first #=>

Regex in ruby to enclose word before : with double quotes

北城以北 提交于 2019-12-11 21:01:55
问题 I have a string which I want to convert in json string Following is the format of the json title: { position: "bottom", text: "Share of Internet Population Growth" }, legend: { visible: false }, chartArea: { background: "" }, seriesDefaults: { type: "donut", startAngle: 150 } To convert it toa JSON string i need to replace the AnyKey: with "AnyKey": I was reading about gsub. What can be exact regex to replace this kind of string Thanks in advance 回答1: (\w+)(?=:) Try this.See demo. http:/