0

I need to create and R function that would take a data frame and column names as arguments (there should be at least two column names in the argument list and maybe more). Then given the data frame, I need to create a json formatted output based the given column names. For example,

this is my df:

structure(list(DateTime = structure(1:8, .Label = c("8/24/2014 15:20", 
"8/24/2014 15:55", "8/24/2014 16:04", "8/24/2014 16:18", "8/24/2014 16:27", 
"8/24/2014 16:42", "8/24/2014 16:56", "8/24/2014 17:10"), class = "factor"), 
    Server1 = c(6.09, 4.54, 5.03, 4.93, 6.27, 4.59, 5.91, 4.53
    ), Server2 = c(5.7, 4.38, 4.52, 4.61, 4.18, 4.61, 4.37, 4.3
    ), Server3 = c(5.21, 5.33, 4.92, 5.56, 5.62, 6.73, 4.76, 
    4.59)), .Names = c("DateTime", "Server1", "Server2", "Server3"
), class = "data.frame", row.names = c(NA, -8L))

I need this function to return this output:

[{"name":"Server1","data":[[18/24/2014 15:20,6.09],[8/24/2014 15:55,4.54],[8/24/2014 16:04,5.03]]},
{"name":"Server2","data":[[18/24/2014 15:20,7.7],[8/24/2014 15:55,4.38],[8/24/2014 16:04,4.52]]},
{"name":"Server3","data":[[18/24/2014 15:20,5.21],[8/24/2014 15:55,5.33],[8/24/2014 16:04,4.92]]}]

Any ideas how I would start with this?

user1471980
  • 10,127
  • 48
  • 136
  • 235

1 Answers1

2

Assuming your data frame is named dd, then

library(rjson)
library(reshape2)

mm <- melt(dd)
ss <- split(mm, mm$variable)

poo <- unname(Map(function(n,x) 
    list(name=n, data=unname(lapply(split(x, 1:nrow(x)), function(x) {
        list(x$DateTime, x$value)
}))), names(ss),ss))
cat(toJSON(poo))

And that gives

[{"name":"Server1","data":[["8/24/2014 15:20",6.09],["8/24/2014 15:55",4.54],["8/24/2014 16:04",5.03],["8/24/2014 16:18",4.93],["8/24/2014 16:27",6.27],["8/24/2014 16:42",4.59],["8/24/2014 16:56",5.91],["8/24/2014 17:10",4.53]]},
{"name":"Server2","data":[["8/24/2014 15:20",5.7],["8/24/2014 15:55",4.38],["8/24/2014 16:04",4.52],["8/24/2014 16:18",4.61],["8/24/2014 16:27",4.18],["8/24/2014 16:42",4.61],["8/24/2014 16:56",4.37],["8/24/2014 17:10",4.3]]},
{"name":"Server3","data":[["8/24/2014 15:20",5.21],["8/24/2014 15:55",5.33],["8/24/2014 16:04",4.92],["8/24/2014 16:18",5.56],["8/24/2014 16:27",5.62],["8/24/2014 16:42",6.73],["8/24/2014 16:56",4.76],["8/24/2014 17:10",4.59]]}]

which seems to match what you wanted. It's not super pretty because you've really gone out of your way to reshape your data in a way that rsjon doesn't necessarily like.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • this is good but my requirment is that it the whole thing needs to be wrapped around a function which will need data frame and column names as arguments. – user1471980 Sep 10 '14 at 16:54
  • 1
    I had assumed that you knew how to write a function in R. I merely focused on the more difficult transformation part. This isn't a made-to-order code writing service. You may customize this code to your specific needs. It would have been better if your question had included how you planned to call this new function or what exactly you would have called to generate the sample output. – MrFlick Sep 10 '14 at 16:58