0

I have a json file that has a multi-layered list (already parsed text). Buried within the list, there is a layer that includes several calculations that I need to average. I have code to do this for each line individually, but that is not very time efficient.

mean(json_usage$usage_history[[1]]$used[[1]]$lift)

This returns an average for the numbers in the lift layer of the list for the 1st row. As mentioned, this isn't time efficient when you have a dataset with multiple rows. Unfortunately, I haven't had much success in using either a loop or lapply to do this on the entire dataset.

This is what happens when I try the for loop:

for(i in json_usage$usage_history[[i]]$used[[1]]$lift){
json_usage$mean_lift <- mean(json_usage$usage_history[[i]]$used[[1]]$lift)
}
Error in json_usage$affinity_usage_history[[i]] : 
  subscript out of bounds

This is what happens why I try lapply:

mean_lift <- lapply(lift_list, mean(lift_list$used$lift))
Error in match.fun(FUN) : 
'mean(lift_list$used$lift)' is not a function, character or symbol
In addition: Warning message:
In mean.default(lift_list$used$lift) :
  argument is not numeric or logical: returning NA

I am new to R, so I know I am likely doing it wrong, but I haven't found any examples of what I'm trying to do. I'm running out of ideas and growing increasingly frustrated. Please help!

Thank you!

Dr_C
  • 1
  • 2
  • Can you share a minimal dataset or the source of the data? See here for help https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Phil Jun 14 '17 at 16:50
  • @Phil No, unfortunately, it contains proprietary and identifiable client information, including the list I am attempting to work with. If I changed every name and tag, it might alter the dataset into an unusable state. – Dr_C Jun 14 '17 at 17:22

1 Answers1

0

The jsonlite package has a very useful function called flatten that you can use to convert the nested lists that commonly appear when parsing JSON data to a more usable dataframe. That should make it simpler to do the calculations you need.

Documentation is here: https://cran.r-project.org/web/packages/jsonlite/jsonlite.pdf

For an answer to a vaguely similar question I asked (though my issue was with NA data within JSON results), see here: Converting nested list with missing values to data frame in R

Sean Norton
  • 277
  • 1
  • 12
  • Thanks for the suggestion, but the json was flattened when it was read into R. It's everything after that where I'm getting stuck. – Dr_C Jun 14 '17 at 17:35