#' Operate columns
#'
#' The function operate_columns() is used to return some columns of a matrix as well as the columns resulting from adding or subtracting columns from each other
#' @keywords internal
#' @param operacions list containig the positions of the columns and/or vectors with operations. e.g. list (1, 3, c(1, -2)) means a matrix with the columns 1, 3 and the column resulting from substracting the 2on column from the 1st.
#' @param columnes MAtrix withthe columns to operate with
#' @return A matrix with the columns asked for
#' @examples
#' input<-matrix(1:10, ncol=5)
#' colnames(input)<-paste("T",1:5,sep="")
#' operations<-list(c(-3,1),c(2,1))
#' operate_columns(operacions=operations, columnes=input)
operate_columns<-function(operacions, columnes, applylog=FALSE){
#save the rownames
keeprownames<-rownames(columnes)
#si la operacio es logaritmica, passem a log NOMES LES COLUMNES AMB LES QUE TREBALLAREM
if(applylog){
affectedcolumns<- unique(abs(unlist(operacions)))
columnes[,affectedcolumns]<-log2(columnes[,affectedcolumns])
#afegim prefixe LOG als noms de les columnes
colnames(columnes)[affectedcolumns]<-paste("LOG(",colnames(columnes)[affectedcolumns],")",sep="")
}
resultat<-matrix(0,ncol = length(operacions),nrow = nrow(columnes),dimnames=list())
colnames(resultat)<-rep("",length(operacions))
#cada loop es una operacio, resultant en una columna a representar
for(i in 1:length(operacions)){
#seleccionem les columnes i posem el signe negatiu a la columne que toca
resultat[,i]<-apply(t(t(columnes[,abs(operacions[[i]])]) * sign(operacions[[i]])),1,sum)
#ARA ENS PREOCUPEM PER CONFORMAR EL NOM DE LES NOVES COLUMNES
nomcolumnes<-colnames(columnes)
#posem simbol neg a les columnes que restarem
nomoperacio<-""
for(j in 1:length(operacions[[i]])){
if(operacions[[i]][j]<0) sign<-"-"
else if (j>1) sign<-"+" #nomes si no es el primer operand cal posar el signe +
else sign<-""
nomoperacio<-paste(nomoperacio,sign,nomcolumnes[abs(operacions[[i]][j])],sep="")
}
#posem el nom definitiu co a nom de la columna resultant de la operacio
colnames(resultat)[i]<-nomoperacio
}
#retuirn the rownames
rownames(resultat)<-keeprownames
return(resultat)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.