2007/12/13

Use scan to read data -- modified code

在做what的設定時 需要是list component.
也就是說當你設了what=list(...)之後 在讀資料時會以一個column一個column來讀, 每個column的屬性就是what所設定的
所以當你把what="character"時 其實是把所有的資料都當做是一個column的character
用例子來說明

example:
原始的資料檔如下
Isat_N43_mA_10/.32,7.1,6.2,5.3
Isat_N4_mA_10/.18,7.35,6.45,5.55
Isat_P43_mA_10/.27,-2.46,-2.91,-3.36
Isat_P4_mA_10/.18,-2.32,-2.71,-3.1

當用scan(infile, what="character",sep=",")
Read 16 items
[1] "Isat_N43_mA_10/.32" "7.1" "6.2" "5.3"
[5] "Isat_N4_mA_10/.18" "7.35" "6.45" "5.55"
[9] "Isat_P43_mA_10/.27" "-2.46" "-2.91" "-3.36"
[13] "Isat_P4_mA_10/.18" "-2.32" "-2.71" "-3.1"


當用scan(infile, what=list("character",bouble(0),double(0),double(0)),sep=",")
讀進去後的結果:

Read 4 records

[[1]]
[1] "Isat_N43_mA_10/.32" "Isat_N4_mA_10/.18" "Isat_P43_mA_10/.27" "Isat_P4_mA_10/.18"

[[2]]
[1] 7.10 7.35 -2.46 -2.32

[[3]]
[1] 6.20 6.45 -2.91 -2.71

[[4]]
[1] 5.30 5.55 -3.36 -3.10

要把資料轉成dataframe 只須用as.data.frame即可(data frame 其實就是vector of list 所以實在是不需要多此一舉的)

修改過的scan
scanData.go <- function(inFileName, colName, colType, headerFg = FALSE, sep = ",", ...){
if(length(colName)>length(colType[!colType == "NULL"])) stop("more column names than columns")
skip <- 0
if(headerFg){
skip <- 1
}
tmp1 <- vector("list",length(colType))
notNull <- which(!colType == "NULL", arr.ind = T)
for(i in 1:length(notNull)){
tmp <- colType[notNull[i]]
switch(match(tmp,c("char","num","logical")),
tmp1[[notNull[i]]] <- "character",
tmp1[[notNull[i]]] <- double(0),
tmp1[[notNull[i]]] <- logical)
}
out <- scan(inFileName, what = tmp1, skip = skip, sep = sep)
out <- out[!(colType == "NULL")]
if(length(colName) < length(out)){
colName <- c(colName, paste("V", 1:(length(out)-length(colName)), sep = ""))
warning("header and 'col.names' are of different lengths")
}
names(out) <- colName
return(out)
}
view raw scanData hosted with ❤ by GitHub

CC Copyright

創用 CC 授權條款
本著作由Chunhung Chou製作,以創用CC 姓名標示-相同方式分享 3.0 Unported 授權條款釋出。