gsub

replace string in dataframe

旧巷老猫 提交于 2019-12-19 03:14:06
问题 I'm trying to replace a certain string in a large data.frame. I just found the following solution but gsub doesn't preserve the original data.frame layout. How can I achieve this. I mean I want to replace a string and don't want to change the layout of the df. Consider this example: test<-data.frame(a=c("a","b","c","d"),b=c("a","e","g","h"),c=c("i","j","k","a")) gsub("a","new",test) Thx 回答1: You will want to lapply through your data.frame testing for character or factor entries and then

Remove special characters from data frame

穿精又带淫゛_ 提交于 2019-12-18 12:34:56
问题 I have a matrix that contains the string "Energy per �m". Before the 'm' is a diamond shaped symbol with a question mark in it - I don't know what it is. I have tried to get rid of it by using this on the column of the matrix: a=gsub('Energy per �m','',a) [and using copy/paste for the first term of gsub], but it does not work.[unexpected symbol in "a=rep(5,Energy per"]. When I try to extract something from the original matrix with grepl I get: 46: In grepl("ref. value", raw$parameter) : input

Ruby post title to slug

允我心安 提交于 2019-12-18 10:27:26
问题 How should I convert a post title to a slug in Ruby? The title can have any characters, but I only want the slug to allow [a-z0-9-_] (Should it allow any other characters?). So basically: downcase all letters convert spaces to hyphens delete extraneous characters 回答1: slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') downcase makes it lowercase. The strip makes sure there is no leading or trailing whitespace. The first gsub replaces spaces with hyphens. The second gsub removes all

Convert numbers to letters

社会主义新天地 提交于 2019-12-18 07:52:50
问题 I have the following vector: x <- c(11, 12, 21, 22) And I want to convert it to the corresponding letters, i.e., I want to get this result: AA AB BA BB How do I make this? I bet there's a simple answer and that it goes through using the reserved LETTERS vector, but I can't figure out a solution. This is the best I've managed to come up with so far (you might want to take the kids out of the room): > paste0(gsub(1, LETTERS[1], substr(x, 1, 1)), gsub(2, LETTERS[2], substr(x, 1, 1))) [1] "A1"

Convert numbers to letters

牧云@^-^@ 提交于 2019-12-18 07:52:37
问题 I have the following vector: x <- c(11, 12, 21, 22) And I want to convert it to the corresponding letters, i.e., I want to get this result: AA AB BA BB How do I make this? I bet there's a simple answer and that it goes through using the reserved LETTERS vector, but I can't figure out a solution. This is the best I've managed to come up with so far (you might want to take the kids out of the room): > paste0(gsub(1, LETTERS[1], substr(x, 1, 1)), gsub(2, LETTERS[2], substr(x, 1, 1))) [1] "A1"

Ruby regex gsub a line in a text file

自古美人都是妖i 提交于 2019-12-18 07:09:44
问题 I need to match a line in an inputted text file string and wrap that captured line with a character for example. For example imagine a text file as such: test foo test bar I would like to use gsub to output: XtestX XfooX XtestX XbarX I'm having trouble matching a line though. I've tried using regex starting with ^ and ending with $, but it doesn't seem to work. Any ideas? I have a text file that has the following in it: test foo test bag The text file is being read in as a command line

Match and replace multiple strings in a vector of text without looping in R

人盡茶涼 提交于 2019-12-18 01:16:26
问题 I am trying to apply gsub in R to replace a match in string a with the corresponding match in string b. For example: a <- c("don't", "i'm", "he'd") b <- c("do not", "i am", "he would") c <- c("i'm going to the party", "he'd go too") newc <- gsub(a, b, c) such that newc = "i am going to the party", "he would go too") This approach does not work because gsub only accepts a string of length 1 for a and b. Executing a loop to cycle through a and b will be very slow since the real a and b have a

Removing Whitespace From a Whole Data Frame in R

可紊 提交于 2019-12-17 18:29:03
问题 I've been trying to remove the white space that I have in a data frame (using R) . The data frame is large (>1gb) and has multiple columns that contains white space in every data entry. Is there a quick way to remove the white space from the whole data frame? I've been trying to do this on a subset of the first 10 rows of data using: gsub( " ", "", mydata) This didn't seem to work, although R returned an output which I have been unable to interpret. str_replace( " ", "", mydata) R returned 47

gsub() in R is not replacing '.' (dot)

人盡茶涼 提交于 2019-12-17 09:33:33
问题 I want to replace dots in "2014.06.09" to "2014-06-09" . I am using gsub() function for it. If x <- "2014.06.09" gsub('2', '-' ,x) # [1] "-014.06.09" But when I try gsub('.', '-', x) # [1] "----------" instead of "2014-06-09" . class(x) # "character" Can some suggest me a way to get this right and also why it is not working for '.' (dot) 回答1: You may need to escape the . which is a special character that means "any character" (from @Mr Flick's comment) gsub('\\.', '-', x) #[1] "2014-06-09" Or

gsub() in R is not replacing '.' (dot)

我们两清 提交于 2019-12-17 09:33:16
问题 I want to replace dots in "2014.06.09" to "2014-06-09" . I am using gsub() function for it. If x <- "2014.06.09" gsub('2', '-' ,x) # [1] "-014.06.09" But when I try gsub('.', '-', x) # [1] "----------" instead of "2014-06-09" . class(x) # "character" Can some suggest me a way to get this right and also why it is not working for '.' (dot) 回答1: You may need to escape the . which is a special character that means "any character" (from @Mr Flick's comment) gsub('\\.', '-', x) #[1] "2014-06-09" Or