gsub

Is There A Neat/Simplest Way To This data.table R Code?

前提是你 提交于 2020-01-21 18:53:06
问题 The STRATUM from OECD data is so long, for simplicity I put this name and would like to simplified it to a more short and precise naming as in the code below. pisaMas[,`:=` (SchoolType = c(ifelse(STRATUM == "National Secondary School", "Public", ifelse(STRATUM == "Religious School", "Religious", ifelse(STRATUM == "MOE Technical School", "Technical",0)))))] pisaMas[,table(SchoolType)] I would like to know if there are a simple way to this problems, using data.table package. 回答1: Current

Is There A Neat/Simplest Way To This data.table R Code?

别来无恙 提交于 2020-01-21 18:52:08
问题 The STRATUM from OECD data is so long, for simplicity I put this name and would like to simplified it to a more short and precise naming as in the code below. pisaMas[,`:=` (SchoolType = c(ifelse(STRATUM == "National Secondary School", "Public", ifelse(STRATUM == "Religious School", "Religious", ifelse(STRATUM == "MOE Technical School", "Technical",0)))))] pisaMas[,table(SchoolType)] I would like to know if there are a simple way to this problems, using data.table package. 回答1: Current

Using gsub function with multiple criteria in R

江枫思渺然 提交于 2020-01-17 03:40:27
问题 Folllow up on question Searching for unique values in dataframe and creating a table with them Here is how my data looks like UUID Source 1 Jane http//mywebsite.com44bb00?utm_source=ADW&utm_medium=banner&utm_campaign=Monk&gclid1234 2 Mike http//mywebsite.com44bb00?utm_source=Google&utm_medium=cpc&utm_campaign=DOG&gclid1234 3 John http//mywebsite.com44bb00?utm_source=Yahoo&utm_medium=banner&utm_campaign=DOG&gclid1234 4 Sarah http//mywebsite.com44bb00?utm_source=Facebookdw&utm_medium=cpc&utm

Force gsub to keep trailing zeros

瘦欲@ 提交于 2020-01-14 22:43:38
问题 I want to extract the digits after "." from data.frame[ ,1] and store them in a second column as following: 1 6.354 354 2 6.355 355 3 6.363 363 4 6.367 367 5 6.378 378 6 6.419 419 7 6.426 426 8 6.427 427 9 6.428 428 10 6.431 431 11 6.460 46 12 6.477 477 (...) To do that I use gsub(".*\\.", "", data.frame[,1]) but this ignores the zeros as you can see e.g. in row 11. How can I extract the complete number/all digits after the "."? The column is numeric. 回答1: If this is numeric, then we may need

How to replace the characters in a string

限于喜欢 提交于 2020-01-14 09:09:48
问题 I have a method that I want to use to replace characters in a string: def complexity_level_two replacements = { 'i' => 'eye', 'e' => 'eei', 'a' => 'aya', 'o' => 'oha'} word = "Cocoa!55" word_arr = word.split('') results = [] word_arr.each { |char| if replacements[char] != nil results.push(char.to_s.gsub!(replacements[char])) else results.push(char) end } end My desired output for the string should be: Cohacohaa!55 However when I run this method it will not replace the characters and only

Ruby gsub function

天大地大妈咪最大 提交于 2020-01-13 07:08:30
问题 I'm trying to create a BBcode [code] tag for my rails forum, and I have a problem with the expression: param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>\1</pre>' ) How do I get what the regex match returns (the text inbetween the [code][/code] tags), and escape all the html and some other characters in it? I've tried this: param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>' + my_escape_function('\1') + '</pre>' ) but it didn't work. It just passes "\1" as a string to the function.

Remove the string before a certain word with R

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-11 10:27:32
问题 I have a character vector that I need to clean. Specifically, I want to remove the number that comes before the word "Votes." Note that the number has a comma to separate thousands, so it's easier to treat it as a string. I know that gsub("*. Votes","", text) will remove everything, but how do I just remove the number? Also, how do I collapse the repeated spaces into just one space? Thanks for any help you might have! Example data: text <- "STATE QUESTION NO. 1 Amendment to Title 15 of the

Remove the string before a certain word with R

不想你离开。 提交于 2020-01-11 10:27:06
问题 I have a character vector that I need to clean. Specifically, I want to remove the number that comes before the word "Votes." Note that the number has a comma to separate thousands, so it's easier to treat it as a string. I know that gsub("*. Votes","", text) will remove everything, but how do I just remove the number? Also, how do I collapse the repeated spaces into just one space? Thanks for any help you might have! Example data: text <- "STATE QUESTION NO. 1 Amendment to Title 15 of the

Applying gsub to various columns

拜拜、爱过 提交于 2020-01-10 18:44:31
问题 What is the most efficient way to apply gsub to various columns? The following does not work x1=c("10%","20%","30%") x2=c("60%","50%","40%") x3 = c(1,2,3) x = data.frame(x1,x2,x3) per_col = c(1,2) x = gsub("%","",x[,per_col]) How can I most efficiently drop the "%" sign in specified columns. Can I apply it to the whole dataframe? This would be useful in the case where I don't know where the percentage columns are. 回答1: You can use apply to apply it to the whole data.frame apply(x, 2, function

Applying gsub to various columns

为君一笑 提交于 2020-01-10 18:43:18
问题 What is the most efficient way to apply gsub to various columns? The following does not work x1=c("10%","20%","30%") x2=c("60%","50%","40%") x3 = c(1,2,3) x = data.frame(x1,x2,x3) per_col = c(1,2) x = gsub("%","",x[,per_col]) How can I most efficiently drop the "%" sign in specified columns. Can I apply it to the whole dataframe? This would be useful in the case where I don't know where the percentage columns are. 回答1: You can use apply to apply it to the whole data.frame apply(x, 2, function