gsub

gsub with “|” character in R

谁说胖子不能爱 提交于 2021-02-17 05:45:31
问题 I have a data frame with strings under a variable with the | character. What I want is to remove anything downstream of the | character. For example, considering the string heat-shock protein hsp70, putative | location=Ld28_v01s1:1091329-1093293(-) | length=654 | sequence_SO=chromosome | SO=protein_coding I wish to have only: heat-shock protein hsp70, putative Do I need any escape character for the | character? If I do: a <- c("foo_5", "bar_7") gsub("*_.", "", a) I get: [1] "foo" "bar" i.e. I

gsub with “|” character in R

妖精的绣舞 提交于 2021-02-17 05:43:05
问题 I have a data frame with strings under a variable with the | character. What I want is to remove anything downstream of the | character. For example, considering the string heat-shock protein hsp70, putative | location=Ld28_v01s1:1091329-1093293(-) | length=654 | sequence_SO=chromosome | SO=protein_coding I wish to have only: heat-shock protein hsp70, putative Do I need any escape character for the | character? If I do: a <- c("foo_5", "bar_7") gsub("*_.", "", a) I get: [1] "foo" "bar" i.e. I

How to remove leading and trailing whitespaces?

主宰稳场 提交于 2021-02-06 10:07:36
问题 I'm using awk '{gsub(/^[ \t]+|[ \t]+$/,""); print;}' in.txt > out.txt to remove both leading and trailing whitespaces. The problem is the output file actually has trailing whitespaces! All lines are of the same length - they are right padded with spaces. What am I missing? UPDATE 1 The problem is probably due to the the fact that the trailing spaces are nor "normal" spaces but \x20 characters (DC4). UPDATE 2 I used gsub (/'[[:cntrl:]]|[[:space:]]|\x20/,"") an it worked. Two strange things:

How do I do an exact string match using gsub in R? [duplicate]

风流意气都作罢 提交于 2021-02-05 09:26:06
问题 This question already has answers here : How to grep a word exactly (3 answers) Closed 5 years ago . raw = c("MOUNTAIN VIEW","MOUNTAIN") x = gsub("MOUNTAIN", "MOUNTAIN VIEW", raw, ignore.case = TRUE) Current output: "MOUNTAIN VIEW VIEW" "MOUNTAIN VIEW" Desired output: "MOUNTAIN VIEW" "MOUNTAIN VIEW" I only want to replace the 2nd entry in the raw data MOUNTAIN with MOUNTAIN VIEW . The first entry in raw data is already correct. But when I do gsub it replaces both the occurrences of MOUNTAIN

Remove all punctuation except underline between characters in R with POSIX character class

点点圈 提交于 2021-02-04 19:45:48
问题 I would like to use R to remove all underlines expect those between words. At the end the code removes underlines at the end or at the beginning of a word. The result should be 'hello_world and hello_world' . I want to use those pre-built classes. Right know I have learn to expect particular characters with following code but I don't know how to use the word boundary sequences. test<-"hello_world and _hello_world_" gsub("[^_[:^punct:]]", "", test, perl=T) 回答1: You can use gsub("[^_[:^punct:]]

Removing curly brackets in R

試著忘記壹切 提交于 2021-02-04 14:06:53
问题 How to remove curly brackets in R? Eg. "{abcd}" to "abcd" How can I use gsub function in R to do this? If any other method is available, please suggest. 回答1: Try this gsub("\\{|\\}", "", "{abcd}") [1] "abcd" Or this gsub("[{}]", "", "{abcd}") 回答2: x <- "{abcd}" gsub("^\\{+(.+)\\}+$", '\\1', x) This will remove all braces on either end of the string. The difference between this and @Dickoa's answer is that this would leave any braces inside the string alone. 回答3: I tend to do it in 2 steps

Applying multiple function via sapply

一个人想着一个人 提交于 2021-01-28 03:55:24
问题 I'm trying to replicate solution on applying multiple functions in sapply posted on R-Bloggers but I can't get it to work in the desired manner. I'm working with a simple data set, similar to the one generated below: require(datasets) crs_mat <- cor(mtcars) # Triangle function get_upper_tri <- function(cormat){ cormat[lower.tri(cormat)] <- NA return(cormat) } require(reshape2) crs_mat <- melt(get_upper_tri(crs_mat)) I would like to replace some text values across columns Var1 and Var2 . The

Substitute vertical lines in Bash

六月ゝ 毕业季﹏ 提交于 2021-01-27 06:49:40
问题 I'm having a hard time finishing my script since there's this part which doesn't function the way I wanted it to be. I have this line in my script: cat /home/tmp/temp1.txt | awk '{gsub("~",RS);gsub("*",RS);print}' > /home/tmp/temp.txt It works fine, yes. But when I do something like this: cat /home/tmp/temp1.txt | awk '{gsub("|",RS);print}' > /home/tmp/temp.txt It's not working at all. I wanted to change all my vertical bars into new line and yet I can't achieve it. Please help me with this.

Substitute vertical lines in Bash

无人久伴 提交于 2021-01-27 06:47:43
问题 I'm having a hard time finishing my script since there's this part which doesn't function the way I wanted it to be. I have this line in my script: cat /home/tmp/temp1.txt | awk '{gsub("~",RS);gsub("*",RS);print}' > /home/tmp/temp.txt It works fine, yes. But when I do something like this: cat /home/tmp/temp1.txt | awk '{gsub("|",RS);print}' > /home/tmp/temp.txt It's not working at all. I wanted to change all my vertical bars into new line and yet I can't achieve it. Please help me with this.

Substitute vertical lines in Bash

人走茶凉 提交于 2021-01-27 06:46:06
问题 I'm having a hard time finishing my script since there's this part which doesn't function the way I wanted it to be. I have this line in my script: cat /home/tmp/temp1.txt | awk '{gsub("~",RS);gsub("*",RS);print}' > /home/tmp/temp.txt It works fine, yes. But when I do something like this: cat /home/tmp/temp1.txt | awk '{gsub("|",RS);print}' > /home/tmp/temp.txt It's not working at all. I wanted to change all my vertical bars into new line and yet I can't achieve it. Please help me with this.