ClusterApply: Applies a function over grouped data

View source: R/ClusterApply.R

ClusterApplyR Documentation

Applies a function over grouped data

Description

Applies a given function to each data dimension separately within each cluster.

Usage

ClusterApply(DataOrDistances,FUN,Cls,Simple=FALSE,...)

Arguments

DataOrDistances

Either a symmetric [1:n,1:n] distance matrix or:

a [1:n,1:d] matrix containing n cases of d-dimensional data points. Every case has d attributes, variables, or features.

FUN

Function to be applied to each cluster of data and each column of data

Cls

[1:n] numerical vector with n numbers defining the classification as the main output of the clustering algorithm. It has k unique numbers representing the arbitrary labels of the clustering.

Simple

Boolean, if TRUE, simplifies output

...

Additional parameters to be passed on to FUN

Details

Applies FUN to each feature within each cluster defined by Cls. If Cls is missing, all observations are assigned to one cluster. The main output contains the result of FUN for each cluster and is named after the supplied function.

For a distance matrix, classical multidimensional scaling is used automatically to obtain coordinates. The number of dimensions is selected from the possible cmdscale output dimensions by minimum stress.

If the name of FUN cannot be extracted, the component is named ResultPerCluster.

Value

If Simple = FALSE, a list with:

UniqueClusters

The unique cluster labels in Cls.

FUNPerCluster

A [1:k,1:d] matrix with one row per cluster and one column per feature. The component name is derived from the supplied FUN; for example, meanPerCluster.

If Simple = TRUE, a [1:k,1:d] matrix with one row per cluster and one column per feature.

Author(s)

Felix Pape, Michael Thrun

Examples

##one dataset
data(Hepta)
Data=Hepta$Data
Cls=Hepta$Cls
#mean per cluster
ClusterApply(Data,mean,Cls)

#Simplified
ClusterApply(Data,mean,Cls,Simple=TRUE)

# Mean per cluster of MDS transformation
# Beware, this is not the same!

ClusterApply(as.matrix(dist(Data)),mean,Cls)


## Not run: 
Iris=datasets::iris
Distances=as.matrix(Iris[,1:4])
SomeFactors=Iris$Species
V=ClusterCreateClassification(SomeFactors)
Cls=V$Cls
V$ClusterNames
ClusterApply(Distances,mean,Cls)

## End(Not run)
#special case of identity
## Not run: 
suppressPackageStartupMessages(library('prabclus',quietly = TRUE))
data(tetragonula)
#Generated Specific Distance Matrix
ta <- alleleconvert(strmatrix=as.matrix(tetragonula[1:236,]))
tai <- alleleinit(allelematrix=ta,distance="none")
Distance=alleledist((unbuild.charmatrix(tai$charmatrix,236,13)),236,13)

MDStrans=ClusterApply(Distance,identity)$identityPerCluster

## End(Not run)

FCPS documentation built on Oct. 3, 2026, 9:06 a.m.