Parsing back to 'messy' API strcuture

Posted by Eric Fail on Stack Overflow See other posts from Stack Overflow or by Eric Fail
Published on 2012-09-12T16:55:39Z Indexed on 2012/09/13 15:38 UTC
Read the original article Hit count: 318

Filed under:
|
|
|

I'm fetching data from an online database (REDcap) via API and the data gets delivered in as comma separated string like this,

RAW.API <- structure("id,event_arm,name,dob,pushed_text,pushed_calc,complete\n\"01\",\"event_1_arm_1\",\"John\",\"1979-05-01\",\"\",\"\",2\n\"01\",\"event_2_arm_1\",\"John\",\"2012-09-02\",\"abc\",\"123\",1\n\"01\",\"event_3_arm_1\",\"John\",\"2012-09-10\",\"\",\"\",2\n\"02\",\"event_1_arm_1\",\"Mary\",\"1951-09-10\",\"def\",\"456\",2\n\"02\",\"event_2_arm_1\",\"Mary\",\"1978-09-12\",\"\",\"\",2\n", "`Content-Type`" = structure(c("text/html", "utf-8"), .Names = c("", "charset")))

I have this script that nicely parses it into a data frame,

(df <- read.table(file = textConnection(RAW.API), header = TRUE, 
sep = ",", na.strings = "", stringsAsFactors = FALSE))
  id     event_arm name        dob pushed_text pushed_calc complete
1  1 event_1_arm_1 John 1979-05-01        <NA>          NA        2
2  1 event_2_arm_1 John 2012-09-02         abc         123        1
3  1 event_3_arm_1 John 2012-09-10        <NA>          NA        2
4  2 event_1_arm_1 Mary 1951-09-10         def         456        2
5  2 event_2_arm_1 Mary 1978-09-12        <NA>          NA        2

I then do some calculations and write them to pushed_text and pushed_calc whereafter I need to format the data back to the messy comma separated structure it came in.

I imagine something like this,

API.back <- `some magic command`(df, ...)

identical(RAW.API, API.back)
[1] TRUE

Some command that can format my data from the data frame I made, df, back to the structure that the raw API-object came in, RAW.API.

Any help would be very appreciated.

© Stack Overflow or respective owner

Related posts about r

    Related posts about api