gsub with “|” character in R

后端 未结 2 461
春和景丽
春和景丽 2021-01-28 20:19

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 ex

2条回答
  •  臣服心动
    2021-01-28 20:47

    Maybe a better job for strsplit than for a gsub

    And yes, it looks like the pipe does need to be escaped.

    string <- "heat-shock protein hsp70, putative | location=Ld28_v01s1:1091329-1093293(-) | length=654 | sequence_SO=chromosome | SO=protein_coding"
    strsplit(string, ' \\| ')[[1]][1]
    

    That outputs

    "heat-shock protein hsp70, putative"
    

    Note that I'm assuming you only want the text from before the first pipe, and that you want to drop the space that separates the pipe from the piece of the string you care about.

提交回复
热议问题