1. zoo. El paquete del zoo tiene una función de combinación de múltiples vías que puede hacer esto de forma compacta. El lapply
convierte cada componente de myList
a un objeto de zoológico y entonces simplemente se funden todos ellos:
# optionally add nice names to the list
names(myList) <- paste("t", seq_along(myList), sep = "")
library(zoo)
fz <- function(x)with(as.data.frame(x, stringsAsFactors=FALSE), zoo(Freq, Var1)))
out <- do.call(merge, lapply(myList, fz))
Los rendimientos por encima de una serie de zoo multivariante en la que los "tiempos" son "a"
, "ago"
, etc. pero si un dato el resultado del cuadro fue deseado, entonces es solo una cuestión de as.data.frame(out)
.
2. Reduce. Aquí hay una segunda solución. Utiliza Reduce
en el núcleo de R.
merge1 <- function(x, y) merge(x, y, by = 1, all = TRUE)
out <- Reduce(merge1, lapply(myList, as.data.frame, stringsAsFactors = FALSE))
# optionally add nice names
colnames(out)[-1] <- paste("t", seq_along(myList), sep = "")
3. xtabs. Éste agrega nombres a la lista y luego extrae las frecuencias, los nombres y los grupos como un vector largo cada uno volver a ponerlos juntos usando xtabs
:
names(myList) <- paste("t", seq_along(myList))
xtabs(Freq ~ Names + Group, data.frame(
Freq = unlist(lapply(myList, unname)),
Names = unlist(lapply(myList, names)),
Group = rep(names(myList), sapply(myList, length))
))
Benchmark
Evaluación comparativa de algunas de las soluciones utilizando la rbenchmark En el paquete obtenemos lo siguiente que indica que la solución de zoo es la más rápida en los datos de muestra y posiblemente también la más simple.
> t1<-table(strsplit(tolower("this is a test in the event of a real word file you would see many more words here"), "\\W"))
> t2<-table(strsplit(tolower("Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal"), "\\W"))
> t3<-table(strsplit(tolower("Ask not what your country can do for you - ask what you can do for your country"), "\\W"))
> myList <- list(t1, t2, t3)
>
> library(rbenchmark)
> library(zoo)
> names(myList) <- paste("t", seq_along(myList), sep = "")
>
> benchmark(xtabs = {
+ names(myList) <- paste("t", seq_along(myList))
+ xtabs(Freq ~ Names + Group, data.frame(
+ Freq = unlist(lapply(myList, unname)),
+ Names = unlist(lapply(myList, names)),
+ Group = rep(names(myList), sapply(myList, length))
+))
+ },
+ zoo = {
+ fz <- function(x) with(as.data.frame(x, stringsAsFactors=FALSE), zoo(Freq, Var1))
+ do.call(merge, lapply(myList, fz))
+ },
+ Reduce = {
+ merge1 <- function(x, y) merge(x, y, by = 1, all = TRUE)
+ Reduce(merge1, lapply(myList, as.data.frame, stringsAsFactors = FALSE))
+ },
+ reshape = {
+ freqs.list <- mapply(data.frame,Words=seq_along(myList),myList,SIMPLIFY=FALSE,MoreArgs=list(stringsAsFactors=FALSE))
+ freqs.df <- do.call(rbind,freqs.list)
+ reshape(freqs.df,timevar="Words",idvar="Var1",direction="wide")
+ }, replications = 10, order = "relative", columns = c("test", "replications", "relative"))
test replications relative
2 zoo 10 1.000000
4 reshape 10 1.090909
1 xtabs 10 1.272727
3 Reduce 10 1.272727
AÑADIDO: second solution.
AGREGADO: tercera solución.
AÑADIDO: benchmark.
oh wait, en mi respuesta asumí que querías una columna de data.frame separada para cada tabla ... ¿Buscabas un formato diferente a ese? –