User Tools

Site Tools


half_matrix

read half matrix data

  • file: read_tag-halfMatrix.r
  • document page: 37


code:

#----open XML package-------------------------------------------------
library(XML)

#----read data between an XML tag-------------------------------------
filename = "D:/Heidi/Master/R_Daten/XML/PairwiseFst_XML2.xml"
tag = "//Fst"
doc = xmlTreeParse(filename, useInternal = TRUE)
ch = getNodeSet(doc, tag)

subDoc = xmlDoc(ch[[1]])
tagData <- xpathApply(subDoc, tag, xmlValue)
free(subDoc)


#----convert string data (half matrix) to a numeric matrix-------------
#----split string----
tagData2 <- as.character(tagData)

tagData3 <- strsplit(tagData2, "\n")
tagMatrix <- as.matrix(as.data.frame(tagData3))
tagMatrix <- tagMatrix[4:nrow(tagMatrix)]
tagMatrix  <- gsub(" + ", " ", tagMatrix)   # trim white space
Data <- strsplit(tagMatrix, " ")

#----to numeric matrix----
Row <- length(Data)

Matrix <- as.matrix(as.data.frame(Data[1]))
Matrix <- rbind(Matrix, matrix(NA, ncol=1, nrow=(Row-1)))
Matrix <- Matrix[3:nrow(Matrix),]
numericList <- as.numeric(Matrix)
numericMatrix <- t(as.matrix(numericList))

for(n in 2:(Row)){
  nextrow <- as.matrix(as.data.frame(Data[n]))
  nextrow <- rbind(nextrow, matrix(NA, ncol=1, nrow=(Row-n)))
  nextrow <- nextrow[3:nrow(nextrow),]
  numericList <- as.numeric(nextrow)
  numericMatrix <- rbind(numericMatrix, t(as.matrix(numericList)))
}
numericMatrix

read half matrix out of full matrix

  • file: read_tag-halfOutOfFullMatrix.r


code:

#----open XML package-----------------------------------------------------------
library(XML)

#----read data between an XML tag-----------------------------------------------
filename = "D:/Master/R_Daten/XML/PairwiseFst_XML2.xml"   #not correct!!!!
tag = "//Fst"
doc = xmlTreeParse(filename, useInternal = TRUE)
ch = getNodeSet(doc, tag)

subDoc = xmlDoc(ch[[1]])
tagData <- xpathApply(subDoc, tag, xmlValue)
free(subDoc)


 #----convert string data (half out of a full matrix) to a numeric matrix------
  #----split string----
  tagData2 <- as.character(xmlText)

  tagData3 <- strsplit(tagData2, "\n")
  tagMatrix <- as.matrix(as.data.frame(tagData3))
  tagMatrix <- gsub(" + ", " ", tagMatrix)   # trim white space

  tagMatrix <- tagMatrix[4:(nrow(tagMatrix)-1)]
  Data <- strsplit(tagMatrix, " ")

  #----to numeric matrix----
  Row <- length(Data)

  Matrix <- matrix(NA, ncol=1, nrow=Row)
  numericList <- as.numeric(Matrix)
  numericMatrix <- t(as.matrix(numericList))

  for(n in 2:(Row)){
    nextrow <- as.matrix(as.data.frame(Data[[n]][3:(n+1)]))
    nextrow <- rbind(nextrow, matrix(NA, ncol=1, nrow=(Row-(n-1))))
    #nextrow <- nextrow[3:nrow(nextrow),]
    numericList <- as.numeric(nextrow)
    numericMatrix <- rbind(numericMatrix, t(as.matrix(numericList)))
  }
  numericMatrix
half_matrix.txt · Last modified: 2008/09/23 09:44 by heidi