での 倒壊 Paket, das kürzlich auf CRAN veröffentlicht wurde, habe ich versucht, die meisten der üblichen Anwendungsfunktionen in nur 2 Funktionen zusammenzufassen:
dapply
(Data-Apply) wendet Funktionen auf Zeilen oder (standardmäßig) Spalten von Matrizen und data.frames an und gibt (standardmäßig) ein Objekt desselben Typs und mit denselben Attributen zurück (es sei denn, das Ergebnis der einzelnen Berechnungen ist atomar und drop = TRUE
). Die Leistung ist vergleichbar mit lapply
für data.frame-Spalten, und etwa 2x schneller als apply
für Matrixzeilen oder -spalten. Die Parallelität ist verfügbar über mclapply
(nur für MAC).
構文です。
dapply(X, FUN, ..., MARGIN = 2, parallel = FALSE, mc.cores = 1L,
return = c("same", "matrix", "data.frame"), drop = TRUE)
Ejemplos:
# Apply to columns:
dapply(mtcars, log)
dapply(mtcars, sum)
dapply(mtcars, quantile)
# Apply to rows:
dapply(mtcars, sum, MARGIN = 1)
dapply(mtcars, quantile, MARGIN = 1)
# Return as matrix:
dapply(mtcars, quantile, return = "matrix")
dapply(mtcars, quantile, MARGIN = 1, return = "matrix")
# Same for matrices ...
BY
ist eine S3-Generik für Split-Apply-Combine-Berechnungen mit Vektor-, Matrix- und Data.frame-Methode. Es ist deutlich schneller als tapply
, by
y aggregate
(よりも速い)。 plyr
大容量データで dplyr
ist jedoch schneller).
構文です。
BY(X, g, FUN, ..., use.g.names = TRUE, sort = TRUE,
expand.wide = FALSE, parallel = FALSE, mc.cores = 1L,
return = c("same", "matrix", "data.frame", "list"))
Ejemplos:
# Vectors:
BY(iris$Sepal.Length, iris$Species, sum)
BY(iris$Sepal.Length, iris$Species, quantile)
BY(iris$Sepal.Length, iris$Species, quantile, expand.wide = TRUE) # This returns a matrix
# Data.frames
BY(iris[-5], iris$Species, sum)
BY(iris[-5], iris$Species, quantile)
BY(iris[-5], iris$Species, quantile, expand.wide = TRUE) # This returns a wider data.frame
BY(iris[-5], iris$Species, quantile, return = "matrix") # This returns a matrix
# Same for matrices ...
Listen von Gruppierungsvariablen können auch an g
.
Apropos Leistung: Ein Hauptziel der 倒壊 ist es, die Hochleistungsprogrammierung in R zu fördern und über Split-Apply-Combine hinauszugehen. Zu diesem Zweck verfügt das Paket über einen vollständigen Satz von schnellen generischen Funktionen auf C++-Basis: fmean
, fmedian
, fmode
, fsum
, fprod
, fsd
, fvar
, fmin
, fmax
, ffirst
, flast
, fNobs
, fNdistinct
, fscale
, fbetween
, fwithin
, fHDbetween
, fHDwithin
, flag
, fdiff
y fgrowth
. Sie führen gruppierte Berechnungen in einem einzigen Durchgang durch die Daten durch (d. h. keine Aufteilung und Neukombination).
構文です。
fFUN(x, g = NULL, [w = NULL,] TRA = NULL, [na.rm = TRUE,] use.g.names = TRUE, drop = TRUE)
Ejemplos:
v <- iris$Sepal.Length
f <- iris$Species
# Vectors
fmean(v) # mean
fmean(v, f) # grouped mean
fsd(v, f) # grouped standard deviation
fsd(v, f, TRA = "/") # grouped scaling
fscale(v, f) # grouped standardizing (scaling and centering)
fwithin(v, f) # grouped demeaning
w <- abs(rnorm(nrow(iris)))
fmean(v, w = w) # Weighted mean
fmean(v, f, w) # Weighted grouped mean
fsd(v, f, w) # Weighted grouped standard-deviation
fsd(v, f, w, "/") # Weighted grouped scaling
fscale(v, f, w) # Weighted grouped standardizing
fwithin(v, f, w) # Weighted grouped demeaning
# Same using data.frames...
fmean(iris[-5], f) # grouped mean
fscale(iris[-5], f) # grouped standardizing
fwithin(iris[-5], f) # grouped demeaning
# Same with matrices ...
In den Paketvignetten stelle ich Benchmarks zur Verfügung. Die Programmierung mit den schnellen Funktionen ist deutlich schneller als die Programmierung mit ドラえもん o データテーブル vor allem bei kleineren Daten, aber auch bei großen Daten.