[R] gsub semicolon with double quotation mark

Ista Zahn istazahn at gmail.com
Wed Jan 25 02:57:44 CET 2012


Hi Assa,

I think you should back up and tell us what you're trying to
accomplish. You can replace characters in the column just as you do in
a vector. But I think that is not what you're trying to do. Are you
trying to split the column into three different columns? If so you can
look at the colsplit function in the reshape2 package.

Best,
Ista

On Tue, Jan 24, 2012 at 8:18 AM, Assa Yeroslaviz <frymor at gmail.com> wrote:
> Hi Ista,
>
> This is hat I thought I am doing
>
> I forgot to mention, but I am working with a table which look like that:
>>head(data)
> id     protein_ids
> 45    FBpp0070037
> 46    FBpp0070039;FBpp0070040
> 47    FBpp0070041;FBpp0070042;FBpp0070043
> 48    FBpp0070044;FBpp0110571
> ...
>
> For one line your solution works fine, but I would like to do it for the
> complete column. I was trying ti use apply on the column, but I get no
> output.
>
> Do you have another idee?
>
> Thanks
> Assa
>
>
> On Tue, Jan 24, 2012 at 13:11, Ista Zahn <istazahn at gmail.com> wrote:
>>
>> 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