How can we get all subsets of a vector's elements?
all.subsets <- function(x, min.n=1, max.n=length(x)){
# return a list with all subsets of x
# Caution: This can be heavy for moderate lengths of x11
lst <- lapply( min.n:max.n, function(i) {
m <- combn(x,i) # this is a matrix, split into it's columns
lapply( seq_len(ncol(m)), function(k) m[,k])
} )
# Alternative:
# lst <- lapply(min.n:max.n, function(i) lapply(apply(combn(x,i),2,list),unlist))
# and now flatten the list of lists into one list
lst <- split(unlist(lst), rep(1:length(idx <- rapply(lst, length)), idx))
return(lst)
}
# example:
y <- letters[1:5]
all.subsets(y)
Mittwoch, 16. Mai 2012
Dienstag, 8. Mai 2012
Run all examples of a package
Get all the base and recommended packages:
installed.packages(priority = c("base","recommended"))
A list of the functions in a package can easily be created by:
ls(pos = "package:MASS")
Run the examples for the first 10 functions of the package MASS:
for( x in paste("example(`", ls(pos="package:MASS")[1:10],"`)", sep="")){
eval(parse(text=x))
}
installed.packages(priority = c("base","recommended"))
A list of the functions in a package can easily be created by:
ls(pos = "package:MASS")
Run the examples for the first 10 functions of the package MASS:
for( x in paste("example(`", ls(pos="package:MASS")[1:10],"`)", sep="")){
eval(parse(text=x))
}
Abonnieren
Posts (Atom)