[R] Converting string to data frame

Bert Gunter bgunter.4567 at gmail.com
Fri Jul 29 02:05:18 CEST 2016


Although others may be better able to decipher your messed up html
post than I, you are more likely to get a helpful response if you
**follow the posting guide** and post in plain text only, using
?dput() to input example data in a form that makes it easy for R
helpers to input your data, scrutinize, and respond.

Possibly useful comment: did you double escape the "|" symbol in your
gsub() call? ("|" must be escaped with a "\" ; but the "\" must be
escaped by a "\" for R to interpret as as a "\" instead of the first
part of an escape code such as e.g.  "\t" for tab).  Example:

> gsub("[[:blank:]]+","\\|","a  b")

[1] "a|b"


Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jul 28, 2016 at 4:38 PM, Steven Kang <stochastickang at gmail.com> wrote:
> Hi R users,
>
> I would like to convert a string into a data frame by creating a separator
> (ie pipe) between each potential fields (then using *read.table* function).
>
> ie. Here is the dummy input data for illustration
> (4 x 5)
>
> Date                      Type                      Description
>                 In            Out        Net
>
> 1/1/2016              Share                    Share margin 1234
> 600         100         500
>
> 1/1/2016              Bond                     Govnt LTM
>                                                 0
>
> 3/1/2016                                              JPY RTOP123
>                 500                         500
>
> 5/1/2016              Cash                      Margin C123
>                                 50           -500
>
> The following *dat* object was read from PDF file and stored as a string
> (which requires to be converted to data frame)
>
>
>> *dat*
>
> [1] “1/01/2016”  Share                          Share margin
> 1234                   600  100  500”
>
> [2]  “1/01/2016”     Bond                       Govt LTM
>                                                 0”
>
> [3]  “3/01/2016”                                       JPY appre RTOP124
>                500          500”
>
> [4]  “5/01/2016”  Cash                           Margin
> call                                             50  -50”
>
>
>> *class(dat)*
>
> [1] “character”
>
> Are there any effective ways (ie functions) to insert a pipe as a separator
> between the fields (including the empty field) like the following desired
> outcome?
>
> [1]  “1/01/2016” | Share|Share margin 1234 |600 | 100 | 500”
>
> [2]  “1/01/2016” | Bond |Govt LTM |||0”
>
> [3]  “3/01/2016” |             |JPY appre RTOP124 |500 ||500”
>
> [4]  “5/01/2016” | Cash  |Margin call ||50 |-50”
>
> I was trying *gsub *function to insert a pipe between the fields and
> everything appeared to be fine except when the pipe wasn’t inserted as
> intended for the empty fields (ie as there are 5 fields, there should be 5
> pipes, but this isn’t the case for records with empty field).
>
> Any suggestion would be much appreciated.
>
>
> Thanks.
>
> Steven
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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