简体   繁体   中英

Parsing and rearranging text data in R to add a tab

I have a text data (mydata.txt) which I will like to rearrange.

mydata.txt:

241623..243414 product="Putative sulfate permease"
complement(344599..354507) product="Alcohol dehydrogenase (EC 1.1.1.1)"
tRNA 168479..169551 product="tRNA-Val-GAC"

My intention was to move the part of every line starting with "product=.." further away from the first part of the text by a tab (\\t) as follows:

241623..243414  product="Putative sulfate permease"
complement(344599..354507)  product="Alcohol dehydrogenase (EC 1.1.1.1)"
tRNA 168479..169551 product="tRNA-Val-GAC"

My attempt so far:

x <- sub("(^\\.)(\\product=\\S+)$","\\1", mydata)
y <- sub("(^\\.)(\\product=\\S+)$","\\2", mydata)

All I'm getting is some numerical values as output in each case. Can someone help me please? Thanks.

Make some test data corresponding to your example:

test <- c(
  "241623..243414 product=\"Putative sulfate permease\"",
  "complement(344599..354507) product=\"Alcohol dehydrogenase (EC 1.1.1.1)\"",
  "tRNA 168479..169551 product=\"tRNA-Val-GAC\""
)

Run the sub function to add a tab ( \\t ):

result <- sub("\\s+(product=)","\t\\1",test)

Results:

> cat(result[1])
241623..243414  product="Putative sulfate permease"> 

> cat(result[2])
complement(344599..354507)      product="Alcohol dehydrogenase (EC 1.1.1.1)"> 

> cat(result[3])
tRNA 168479..169551     product="tRNA-Val-GAC"> 

In case you want to do that for every product= you may just

library(stringr)
x <- '241623..243414  product="Putative sulfate permease"
      complement(344599..354507)  product="Alcohol dehydrogenase (EC 1.1.1.1)"
      tRNA 168479..169551 product="tRNA-Val-GAC"'
str_replace_all(x, "product=", "\tproduct=")

This will give you

241623..243414      product="Putative sulfate permease"
complement(344599..354507)      product="Alcohol dehydrogenase (EC 1.1.1.1)"
tRNA 168479..169551     product="tRNA-Val-GAC"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM