0

I am using the jsonlite package to work with a complex json file in r. To give a sense of its complexity, here is a look into two different data frames it contains

> output$dataobjects[1]$dataobject$scoredresults$x
        xid rawscore tscore percentile standarderror omit
  1   LAC.1        0      0          0             0    0
  2   LAC.5        0      0          0             0    0
  3   LAC.9        0      0          0             0    0
  4  LAC.14        0      0          0             0    0

> output$dataobjects[1]$dataobject$scoredresults$y
       yid rawscore tscore percentile standarderror omit
1   c1.1.1        0      0          0             0    0
2   c1.1.3        0      0          0             0    0
3   c1.1.5        0      0          0             0    0
4   c1.1.6        0      0          0             0    0

I would like to dynamically edit this json based on an external dataframe. Say the dataframe looks like this

id <- c("LAC.1", "LAC.5", "c1.1.1", "c1.1.5", "LAC.14")
rawscore <- c(15, 10, 12, 14, 15)
type <- c("x", "x", "y", "y", "x")
df <- data.frame(id, rawscore, type)

I want to use a for-loop to iterate over the rows in this dataframe, updating the rawscore column in the json as I go. I can't figure out how to move from x to y within the json while staying within the for-loop. Please help!

ADF
  • 522
  • 6
  • 14
  • It is unclear what you are trying to do. It would be easier to help you if you provided a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and also clearly showed the desired output. Share your data in a way that's easy to copy/paste into R (like `dput()`. – MrFlick Jan 10 '18 at 16:14
  • 1
    Why not make the edits in the data frame, and convert back to json? – Cybernetic Jan 10 '18 at 16:24
  • @Cybernetic that is almost certainly the best solution. Not sure why I didn't think to do that initially. – ADF Jan 10 '18 at 16:32

0 Answers0