【发布时间】:2014-11-05 19:55:37
【问题描述】:
我有这个 R 函数。第一个参数是 json 数据对象。我将 json 对象转换为数据框,然后根据给定此函数的其他参数,我根据给定的参数对数据框进行子集化。参数可以是数据框中列名的全部或子集,因此会有所不同。
转换为自定义 json 数据
data<-function(x, ...) {
x<-fromJSON(x)
(x <- lapply(x, function(x) { as.data.frame(x) }))
(x <- do.call(rbind, x))
x<-unique(x)
cols <- c(...)
dd<-cbind(Date=x[,1],subset(x, select=cols))
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$Date, x$value)
}))), names(ss),ss))
p<-toJSON(poo)
return(p)
}
我的问题是这样的。给定一个可以采用不同数量参数的函数,我如何引用这些参数?
例如,我有这个数据帧:
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"
), row.names = c("DateTime.1", "DateTime.2", "DateTime.3", "DateTime.4",
"DateTime.5", "DateTime.6", "DateTime.7", "DateTime.8"), class = "data.frame")
我需要调用我的数据函数,
data(x, "DateTime","Server1")
也许其他时间:
data(x, "Datetime", "Server1", "Server2")
有没有办法传递函数参数列表?
例如:
arg_list<-c("DateTime", "Server1", "Server3")
data(x, arg_list)
【问题讨论】:
标签: r