0

我有一个数据框(MS Office 多年来的故障计数),我用它来成功生成 sparktable:

df_Office_final_sparktable
component   faults time
Excel       2      2001
Excel       1      2002
Excel       5      2003
Excel       5      2004
Excel       5      2005
Excel       6      2006
Excel       0      2007
Excel       0      2008
Excel       0      2009
Excel       0      2010
Excel       0      2011
Excel       0      2012
PPT         1      2001
PPT         1      2002
PPT         1      2003
PPT         1      2004
PPT         2      2005
PPT         3      2006
PPT         0      2007
PPT         0      2008
PPT         0      2009
PPT         0      2010
PPT         0      2011
PPT         0      2012
Word        5      2001
Word        4      2002
Word        3      2003
Word        1      2004
Word        3      2005
Word        2      2006
Word        5      2007
Word        3      2008
Word        0      2009
Word        1      2010
Word        0      2011
Word        0      2012

下面是对应的代码: Office_content<-list(

2001=function(x) { head(x,1) },
2012=function(x) { tail(x,1) },
Office_content[['Office Trend']]<-newSparkLine())
Office_varType<-rep("outages",3)  df_Office_final_sparktable<-df_Office_final_sparktable[,c("component","faults","time")]  df_Office_final_sparktable$time<-as.numeric(as.character(df_Office_final_sparktable$time))     Office_dat<-reshapeExt(df_Office_final_sparktable,idvar="component",varying=list(2))
Office_sparkTab<-newSparkTable(Office_dat,Office_content,Office_varType)
showSparkTable (Office_sparkTab , outputType = "html", filename = "t1")

如您所见,我可以使用 head 命令和相应的迷你图输出 2001 年和 2012 年故障表。但是我似乎无法弄清楚如何输出 2002 年至 2011 年(含)的故障总数,我尝试使用以下代码从数据框中提取第二行:

df_Office_final_sparktable[2:2, 1:3]

我意识到这是不正确的,因为我无法将此命令映射回一个函数,所以我想知道我是否需要编写 10 个单独的函数来调用每个办公室所需的故障总数组件年复一年?

在此先感谢,乔纳森

4

1 回答 1

0

好的,我终于想通了。我必须创建 10 个函数来从数据框中提取第二行,如下所示:

head_row2_Office <- function(x) {
result <- subset(x, time =='2002' & component =="Excel", select=c(1:3))
return (result)
}

之后我不得不像这样调用列表函数中的函数:

Office_content<-list(
Jan=function(x) { head(x,1) },
Feb=function(x) { head_row2_Office(x) },
Mar=function(x) { head_row3_Office(x) },
Apr=function(x) { head_row4_Office(x) },
May=function(x) { head_row5_Office(x) },
Jun=function(x) { head_row6_Office(x) },
Jul=function(x) { head_row7_Office(x) },
Aug=function(x) { head_row8_Office(x) },
Sep=function(x) { head_row9_Office(x) },
Oct=function(x) { head_row10_Office(x) },
Nov=function(x) { head_row11_Office(x) },
Dec=function(x) { tail(x,1) },
Office_content[['Office Trend']]<-newSparkLine()
)

Office_varType<-rep("outages",13) # set the tables columns to 13 for table + graph
df_Office_final_sparktable<-df_Office_final_sparktable[,c("component","faults","time")]
df_Office_final_sparktable$time<-as.numeric(as.character(df_Office_final_sparktable$time))
Office_dat<-reshapeExt(df_Office_final_sparktable,idvar="component",varying=list(2))
Office_sparkTab<-newSparkTable(Office_dat,Office_content,Office_varType)
showSparkTable (Office_sparkTab , outputType = "html", filename = "t1")

于 2016-07-22T10:15:54.543 回答