楼主: 
LCL2 (新年快热~)   
2005-03-15 19:38:58※ 引述《mapleV (Sucks)》之铭言:
: ※ 引述《untitled (Causality)》之铭言:
: : !!! How's the CPU of your computer?
:         PIII 733Mhz for executing the transformation from loaded data to
:         matrix. 3x seconds.
:         Loading data takes 8~10 seconds.
: : Would you like to share your code with us next Monday?
:         I think it's not necessary to show my code next Monday.
:         My code is customized for usps data. Not very general.
:         Just a simple idea.
:         If there exists any errors, please remark it.
:         load_transform <- function(filename , ncolumn)
:         {
:         mdt <- readLines(filename)
:         temp <- list()
:         for (i in 1: length(mdt))
:         temp[[i]] <- as.numeric(matrix(strsplit(mdt[i], " |:")[[1]][2*c(1:ncolumn)-1]))
:         dx <- matrix(,ncol = ncolumn ,nrow = length(mdt))
:         for (i in 1:length(mdt))
:         dx[i,] <- temp[[i]]
:         dx
:         }
I did some modify to the above code for index support. It takes
 3x sec on 1.2G AMD CPU for usps loading. But I don't think it's fast
enough for hw3 still XD. Is there any other way? (any code problem please
tell me, thanks)
readData <- function (filename, ncolumn){
    lines <- readLines(filename)
    linenum <- length(lines)
    valueIndex <- 2*(1:ncolumn)-1
    indexIndex <- 2*(1:ncolumn-1)
    dataMatrix <- matrix(0,linenum, ncolumn)
    for(i in 1:linenum){
        tmp <- as.numeric(matrix(strsplit(lines[i], " +|:", perl=TRUE)[[1]]))
        dataTmp <- tmp[valueIndex]
        indexTmp <- tmp[indexIndex]
        iUsedIndex <- !is.na(indexTmp)
        dUsedIndex <- !is.na(dataTmp)
        dataMatrix[i, c(1, 1+indexTmp[iUsedIndex])] <- dataTmp[dUsedIndex]
    }
    return(dataMatrix)
}
tmp <- readData("usps", 257)