我有一个数据帧( 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个单独的函数,我需要调用这些函数才能每年提取每个办公组件所需的故障总数?
先谢谢你,乔纳森
发布于 2016-07-22 18:15:55
好了,我终于弄明白这个问题了。我必须创建10个函数来从数据框中拉出第二行,如下所示:
head_row2_Office <- function(x) {
result <- subset(x, time =='2002' & component =="Excel", select=c(1:3))
return (result)
}在那之后,我不得不像这样调用list函数中的函数:
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")
https://stackoverflow.com/questions/38461284
复制相似问题