[[Rの備忘録]]
古い記事だがhttp://tolstoy.newcastle.edu.au/R/e2/help/06/10/3064.html に以下のような関数がある
expand.dft <- function(x, na.strings = "NA", as.is = FALSE, dec = ".") {
# Take each row in the source data frame table and replicate it
# using the Freq value
DF <- sapply(1:nrow(x), function(i) x[rep(i, each = x$Freq[i]), ], simplify = FALSE)
# Take the above list and rbind it to create a single DF
# Also subset the result to eliminate the Freq column
DF <- subset(do.call("rbind", DF), select = -Freq)
# Now apply type.convert to the character coerced factor
# columns # to facilitate data type selection for each column
DF <- as.data.frame(lapply(DF,
function(x)
type.convert(as.character(x),
na.strings = na.strings,
as.is = as.is,
dec = dec)))
# Return data frame
DF
}
これを応用したのだと思う関数が[[NCStatsパッケージ:http://www.ncfaculty.net/dogle/R/FAQ/install_NCStats.html]] の expandTable 関数に実装されている.
> source("http://www.rforge.net/NCStats/InstallNCStats.R")
> library(NCStats)
> ?NCStats
> expandTable
function (x, var.names = NULL, ...)
{
x <- as.data.frame.table(x)
df <- sapply(1:nrow(x), function(i) x[rep(i, each = x[i,
"Freq"]), ], simplify = FALSE)
df <- subset(do.call("rbind", df), select = -Freq)
for (i in 1:ncol(df)) {
df[[i]] <- type.convert(as.character(df[[i]]), ...)
}
rownames(df) <- NULL
if (!is.null(var.names)) {
if (length(var.names) < dim(df)[2])
stop("Too few var.names given.")
else if (length(var.names) > dim(df)[2])
stop("Too many var.names given.")
else names(df) <- var.names
}
df
}
<environment: namespace:NCStats>