R 通过聚合实现数据帧中矩阵的组合
我试图通过因子计算分位数,并使用xtable将得到的聚合打印成latex格式。不幸的是,我的行为有些古怪。希望有一个干净的解决方案 要创建一个示例,请执行以下操作:R 通过聚合实现数据帧中矩阵的组合,r,matrix,dataframe,xtable,R,Matrix,Dataframe,Xtable,我试图通过因子计算分位数,并使用xtable将得到的聚合打印成latex格式。不幸的是,我的行为有些古怪。希望有一个干净的解决方案 要创建一个示例,请执行以下操作: tm <- data.frame(f=c("a","b","c"),v=runif(30)) tm$f <- factor(tm$f) agv <- aggregate(v~f,tm, quantile) 给予 cols[,i+pos]中的错误一种稍微更直接的方法(虽然在概念上与您已经做的没有太大的不同)可能是使
tm <- data.frame(f=c("a","b","c"),v=runif(30))
tm$f <- factor(tm$f)
agv <- aggregate(v~f,tm, quantile)
给予
cols[,i+pos]中的错误一种稍微更直接的方法(虽然在概念上与您已经做的没有太大的不同)可能是使用
do.call(data.frame,…)
。以下内容适合我
xtable(do.call(data.frame, c(agv, check.names = FALSE)))
对我来说,这意味着:
> xtable(do.call(data.frame, c(agv, check.names = FALSE)))
% latex table generated in R 3.0.0 by xtable 1.7-1 package
% Thu Apr 25 11:10:26 2013
\begin{table}[ht]
\centering
\begin{tabular}{rlrrrrr}
\hline
& f & v.0\% & v.25\% & v.50\% & v.75\% & v.100\% \\
\hline
1 & a & 0.06 & 0.27 & 0.38 & 0.64 & 0.94 \\
2 & b & 0.20 & 0.38 & 0.52 & 0.70 & 0.87 \\
3 & c & 0.01 & 0.22 & 0.60 & 0.87 & 0.99 \\
\hline
\end{tabular}
\end{table}
xtable
也可用于数据。表
s,因此您还可以执行以下操作:
library(data.table)
DT <- data.table(tm, key = "f")
xtable(DT[, as.list(quantile(v)), by = key(DT)])
库(data.table)
DT
cbind(f=as.character(agv$f), data.frame(agv$v,check.names=F))
\begin{table}[ht]
\centering
\begin{tabular}{rlrrrrr}
\hline
& f & 0\% & 25\% & 50\% & 75\% & 100\% \\
\hline
1 & a & 0.00 & 0.25 & 0.48 & 0.75 & 0.99 \\
2 & b & 0.00 & 0.28 & 0.46 & 0.74 & 1.00 \\
3 & c & 0.02 & 0.21 & 0.44 & 0.63 & 1.00 \\
\hline
\end{tabular}
\end{table}
xtable(do.call(data.frame, c(agv, check.names = FALSE)))
> xtable(do.call(data.frame, c(agv, check.names = FALSE)))
% latex table generated in R 3.0.0 by xtable 1.7-1 package
% Thu Apr 25 11:10:26 2013
\begin{table}[ht]
\centering
\begin{tabular}{rlrrrrr}
\hline
& f & v.0\% & v.25\% & v.50\% & v.75\% & v.100\% \\
\hline
1 & a & 0.06 & 0.27 & 0.38 & 0.64 & 0.94 \\
2 & b & 0.20 & 0.38 & 0.52 & 0.70 & 0.87 \\
3 & c & 0.01 & 0.22 & 0.60 & 0.87 & 0.99 \\
\hline
\end{tabular}
\end{table}
library(data.table)
DT <- data.table(tm, key = "f")
xtable(DT[, as.list(quantile(v)), by = key(DT)])