Thursday, November 5, 2015

Dummy meets JSON – II


Before I tried out my dumb approach shown in my last post, I guess I had the workable idea to flatten the second column whose name is “apis” and which is a list of 102 data frames each with two variables, namely, “name” and “url”, with variable number of rows.

To do so, I need to find way to convert the list of 102 data frames into a single data frame and then combine this with the first column of the original data frame. For that I found this work perfectly:
This single data frame consisted of 421 rows and to combine with x$name which has 102 rows, I could create an id column in the collapsed data frame and using row.names of x as integer I could then merge the two data frames. Then I could extract the appropriate columns of the merged data frame to write to a csv file. The problem was that I didn't know how to add id to the collapsed data frame. That's why I resorted to my dumb approach given in my last post.

Now I found that I could add id to the list of data frames x$apis (or x[[2]]) with:
That let me get a not-so-dumb solution as follows:

But wait, the R gurus would like to see something more elegant than this. Instead of the “for” loop in line number 9 of my script, they would like to see something that is not using a loop. Well, it is beyond my reach right now.


No comments:

Post a Comment