R根据一个变量创建数据框的topcount

时间:2016-04-18 13:07:49

标签: r dataframe

我有一个包含多列的数据框,其中一列是错误代码。除错误代码外,还有严重性代码(A到E)。我想创建一个包含前10个频繁错误代码的矩阵,以及严重性代码(以及可能的其他变量)。我怎么能这样做?

输入:

 18-Apr-2016 15:29:01.028 SEVERE [http-nio-8084-exec-151] org.apache.catalina.core.StandardContext.listenerStop Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener 
    java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 
    at org.springframework.web.context.ContextCleanupListener.<clinit>(ContextCleanupListener.java:43) 
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:144) 
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4785) 
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5404) 
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160) 
    at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1293) 
    at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:366) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333) 
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167) 
    ... 37 more

预期产出:

| Error code | Severity code | Description
    1              A
    2              A
    1              A
    3              B
    3              B
    1              A

2 个答案:

答案 0 :(得分:2)

可以使用data.table绝对快速完成。

假设:data.frame保存在变量df中,列名为Error_CodeSeverity_Code

library(data.table)

## converts data.frame to data.table
setDT(df)

## The only line you have to write
df[,.N,by=c('Error_Code','Severity_Code')]

##   Error_Code Severity_Code N
##1:          1             A 3
##2:          2             A 1
##3:          3             B 2

答案 1 :(得分:1)

您的数据:

Error_code <- c(1,2,1,3,3,1)
   LL <- data.frame(Error_code,Severity_code,stringsAsFactors=F)
   Severity_code <- c("A","A","A","B","B","A")

解决方案,代码那样,你可以安装这个软件包“plyr”,然后使用函数“count”

install.packages("plyr") 
library(plyr)
Freq_table  <- count(LL,vars=c("Error_code","Severity_code"))
colnames(Freq_table) <- c("Error code","Severity code","Frequency")

结果

 Freq_table