/working/auteur/R/intercalate.samples.R

http://github.com/eastman/auteur · R · 38 lines · 32 code · 4 blank · 2 comment · 9 complexity · d0cee096712e7fa4c0b38ecaf76b48c0 MD5 · raw file

  1. #general utility for combining supplied list of dataframes into a single 'intercalated' sample. E.g., list(as.data.frame(c(AAA)),as.data.frame(c(BBB))) becomes data.frame(c(ABABAB))
  2. #author: JM EASTMAN 2010
  3. intercalate.samples <-
  4. function(list.obj) {
  5. if(length(list.obj)<2) warning("Nothing to be done... too few objects in list.")
  6. vv=sapply(list.obj, is.vector)
  7. if(all(vv)) list.obj=lapply(list.obj, function(x) {y=data.frame(x); names(y)=NULL; return(y)})
  8. orig.names=names(list.obj[[1]])
  9. n=length(list.obj)
  10. R=sapply(list.obj, nrow)
  11. if(length(unique(R))!=1) {
  12. minR=nrow(list.obj[[which(R==min(R))]])
  13. list.obj=lapply(list.obj, function(x) {y=as.data.frame(x[1:minR,]); names(y)=orig.names; return(y)})
  14. warning("Objects pruned to the length of the smallest.")
  15. }
  16. C=sapply(list.obj, ncol)
  17. if(length(unique(C))!=1) stop("Cannot process objects; column lengths do not match.")
  18. c=unique(C)
  19. if(c>1) {
  20. M=lapply(1:length(list.obj), function(x) mm=match(names(list.obj[[x]]), orig.names))
  21. for(mm in 2:length(list.obj)) {
  22. list.obj[[mm]]=list.obj[[mm]][,M[[mm]]]
  23. }
  24. }
  25. r=nrow(list.obj[[1]])
  26. indices=lapply(1:n, function(x) seq(x, r*n, by=n))
  27. out.array=array(dim=c(r*n, c))
  28. for(nn in 1:n){
  29. out.array[indices[[nn]],]=as.matrix(list.obj[[nn]])
  30. }
  31. out.array=data.frame(out.array)
  32. if(!is.null(orig.names)) names(out.array)=orig.names else names(out.array)=paste("X",1:ncol(out.array),sep="")
  33. return(out.array)
  34. }