[R] gsub semicolon with double quotation mark

Ista Zahn istazahn at gmail.com
Tue Jan 24 13:11:53 CET 2012


Hi Assa,

On Tue, Jan 24, 2012 at 6:30 AM, Assa Yeroslaviz <frymor at gmail.com> wrote:
> Hi,
>
> I would like to substitute a semicolon with two double quotation marks and
> a comma inbetween.
> It suppose to look like that:
>
> I have:
> FBpp0070086;FBpp0099643;FBpp0112915
> I would like to have:
> "FBpp0070086","FBpp0099643","FBpp0112915"
>
> I tried with various numbers of backslashes, but noe have worked.
> for example:
> gsub(";", "\\\",\"", data$protein_ids)
> gsub(";", "\\",\"", data$protein_ids)
> gsub(";", "\",\"", data$protein_ids)
This is actually the one you want. You may be getting confused by the
fact that print does some formatting of strings, including enclosing
them in quotes. To see what you've actually got here, try out the
cat() function:

x <- "FBpp0070086;FBpp0099643;FBpp0112915"
cat(gsub(";", "\",\"", x))
FBpp0070086","FBpp0099643","FBpp0112915

So that's what you said you wanted, although I expect you also want
quotes at the beginning and end of the vector. I also tend to
alternate " and ' instead of escaping quotes (only works once of
course, but usually that's all you need), so:

cat(paste('"', gsub(";", '",\"', x), '"', sep = ""))
"FBpp0070086","FBpp0099643","FBpp0112915"

Best,
Ista



> gsub(";", "\\',\\'", data$protein_ids)
> gsub(";", '"',data$protein_ids)
>
> What do I miss?
>
> Thanks
> Assa
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list