# NOT RUN {
########################################################################
# EXAMPLE 1:
# The code simulates a data frame (called dat1) of correlated rows.
# You can skip this part and start at the line called Typical Input Data
# The first column of the data frame will contain row identifiers
# number of columns (e.g. observations or microarrays)
m=60
# number of rows (e.g. variables or probes on a microarray)
n=500
# seed module eigenvector for the simulateModule function
MEtrue=rnorm(m)
# numeric data frame of n rows and m columns
datNumeric=data.frame(t(simulateModule(MEtrue,n)))
RowIdentifier=paste("Probe", 1:n, sep="")
ColumnName=paste("Sample",1:m, sep="")
dimnames(datNumeric)[[2]]=ColumnName
# Let us now generate a data frame whose first column contains the rowID
dat1=data.frame(RowIdentifier, datNumeric)
#we simulate a vector with n/5 group labels, i.e. each row group corresponds to 5 rows
rowGroup=rep( paste("Group",1:(n/5), sep=""), 5 )
# Typical Input Data
# Since the first column of dat1 contains the RowIdentifier, we use the following code
datET=dat1[,-1]
rowID=dat1[,1]
# assign row names according to the RowIdentifier
dimnames(datET)[[1]]=rowID
# run the function and save it in an object
collapse.object=collapseRows(datET=datET, rowGroup=rowGroup, rowID=rowID)
# this creates the collapsed data where
# the first column contains the group name
# the second column reports the corresponding selected row name (the representative)
# and the remaining columns report the values of the representative row
dat1Collapsed=data.frame( collapse.object$group2row, collapse.object$datETcollapsed)
dat1Collapsed[1:5,1:5]
########################################################################
# EXAMPLE 2:
# Using the same data frame as above, run collapseRows with a user-inputted function.
# In this case we will use the mean. Note that since we are choosing some combination
# of the probe values for each gene, the group2row and selectedRow output
# parameters are not meaningful.
collapse.object.mean=collapseRows(datET=datET, rowGroup=rowGroup, rowID=rowID,
method="function", methodFunction=colMeans)[[1]]
# Note that in this situation, running the following code produces the identical results:
collapse.object.mean.2=collapseRows(datET=datET, rowGroup=rowGroup, rowID=rowID,
method="Average")[[1]]
########################################################################
# EXAMPLE 3:
# Using collapseRows to calculate the module eigengene.
# First we create some sample data as in example 1 (or use your own!)
m=60
n=500
MEtrue=rnorm(m)
datNumeric=data.frame(t(simulateModule(MEtrue,n)))
# In this example, rows are genes, and groups are modules.
RowIdentifier=paste("Gene", 1:n, sep="")
ColumnName=paste("Sample",1:m, sep="")
dimnames(datNumeric)[[2]]=ColumnName
dat1=data.frame(RowIdentifier, datNumeric)
# We simulate a vector with n/100 modules, i.e. each row group corresponds to 100 rows
rowGroup=rep( paste("Module",1:(n/100), sep=""), 100 )
datET=dat1[,-1]
rowID=dat1[,1]
dimnames(datET)[[1]]=rowID
# run the function and save it in an object
collapse.object.ME=collapseRows(datET=datET, rowGroup=rowGroup, rowID=rowID, method="ME")[[1]]
# Note that in this situation, running the following code produces the identical results:
collapse.object.ME.2 = t(moduleEigengenes(expr=t(datET),colors=rowGroup)$eigengene)
colnames(collapse.object.ME.2) = ColumnName
rownames(collapse.object.ME.2) = sort(unique(rowGroup))
# }
Run the code above in your browser using DataLab