在文件夹中的一个文件上运行此脚本时:
emboss<-read.table("emboss_012.ss",header=T)
x<-table(emboss[,2],emboss[,3])/NROW(emboss[,3])
y<-as.vector(t(x))
nms <- expand.grid(colnames(x), rownames(x))
names(y) <- paste( nms[,2],nms[,1],sep="")
write.table(t(y), file = "test3.csv",append=TRUE)
我得到了理想的结果
然而,对文件夹中的所有文件一次性执行此操作会导致随机NA出现。我这样做是通过:
runForAll <- function(x) {
emboss <- read.table(x,header=T)
x <- table(emboss[,2],emboss[,3])/NROW(emboss[,3])
y <- as.vector(t(x))
nms <- expand.grid(colnames(x), rownames(x))
names(y) <- paste( nms[,2],nms[,1],sep="")
return(t(y))
}
my.files <- list.files(pattern = "emboss_\\d+\\.ss")
outputs <- lapply(my.files, FUN = runForAll)
library(plyr)
one.header.output <- rbind.fill.matrix(outputs)
write.table(one.header.output, file = "nontpsec.csv")
我的文件位于:
https://drive.google.com/folderview?id=0B0iDswLYaZ0zWjQ4RjdnMEUzUW8&usp=sharing
这非常奇怪,不能解释为什么会发生这种情况,尤其是所有其他数据都是正确的,即使在一次性循环遍历所有文件时也是如此。
答案 0 :(得分:2)
您的数据表长度不同,例如第一个有20行,最后一个只有19个! 这就是问题所在。
这是一个小测试:
tmp <- c("A", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "Y")
which(rownames(x) %in% tmp)
对于文件12和13,缺少第二行(标签B)。
看一下这篇文章:
Compare two data.frames to find the rows in data.frame 1 that are not present in data.frame 2
这可能对您有用: