简体   繁体   English

如何从R函数以数据框和列名作为参数创建自定义json输出

[英]how do you create a custom json output from an R function with dataframe and column names as arguments

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). 我需要创建一个R函数,该函数将数据框和列名作为参数(参数列表中至少应有两个列名,也许还有更多)。 Then given the data frame, I need to create a json formatted output based the given column names. 然后给定数据框,我需要基于给定的列名创建一个json格式的输出。 For example, 例如,

this is my df: 这是我的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? 有什么想法我将如何开始吗?

Assuming your data frame is named dd , then 假设您的数据帧名为dd ,则

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. 它不是很漂亮,因为您真的已经用RSJON不一定喜欢的方式来重塑数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 R 中,如何创建一个将参数计算为字符串的函数? - In R, how do you create a function that evaluates arguments as strings? 如何将多个列从 dataframe 作为个人 arguments 传递到 R 中的自定义 function - How do I pass multiple columns from a dataframe as individual arguments to a custom function in R 如何从数据框的列中的函数获取输出-R - How to get the output from the function in a column of a dataframe - R 如何从R函数返回输出 - How do you return output from R function 你如何操纵R的cuminc函数的输出 - How do you manipulate the output from R's cuminc function 如何使用 R 中的特定列名构建一个空的 dataframe 的自定义 function - How to make a custom function that builds an empty dataframe with specific column names in R 如何将 output 和 dataframe 作为文本(字符串)混合值和列名 R - How to output a dataframe as text (string) mixing values and column names R 使用函数参数更改R输出对象名称 - Changing R output object names with function arguments R-如何在函数中使用列名作为参数并插入模型公式 - R - how to use column names as arguments in a function and insert into a model formula 如何创建仅包含来自另一个数据框的某些行且具有列名的数据框? - How do I create a dataframe with only some rows from another dataframe, with column names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM