[R] Converting string to data frame

Steven Kang stochastickang at gmail.com
Fri Jul 29 01:38:26 CEST 2016


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]]



More information about the R-help mailing list