假设我们在目录中有文件列表,并且想要检查其他文件中是否存在ref
值。如果数字存在,则输出0
,如果不是1
。
writeFiles <- function(n, maxRows=10){
lapply(1:n,function(x) write.table(sample(sample(maxRows)[1],replace=F),paste(x,'.txt',sep=""), quote=FALSE, col.names = FALSE,row.names=FALSE))
}
writeFiles(10,10)
这会在工作目录中创建1.txt,2.txt,3.txt ... 10.txt文件。
filesToProcess <- dir(pattern = "*\\.txt")
listofFiles <- lapply(filesToProcess, function(x) read.table(x, header = F))
listofFiles
[[1]]
V1
1 2
2 3
3 4
4 1
[[2]]
V1
1 1
[[3]]
V1
1 1
[[4]]
V1
1 4
2 1
3 5
4 2
5 3
[[5]]
V1
1 3
2 2
3 4
4 1
[[6]]
V1
1 4
2 5
3 2
4 3
5 1
[[7]]
V1
1 1
2 2
3 4
4 3
[[8]]
V1
1 3
2 5
3 6
4 2
5 4
6 7
7 1
[[9]]
V1
1 1
[[10]]
V1
1 1
2 3
3 2
期望的输出
注意:号码的位置无关紧要。
ref<-seq(1:10)
ref 1.txt 2.txt ...... 10.txt
1 1 0 0
2 0 1 0
3 0 0 0
4 1 0 1
5 1 0 1
6 1 1 0
7 0 0 1
8 1 1 0
9 0 0 1
10 1 0 1
答案 0 :(得分:2)
我们可以尝试
cbind(ref, sapply(lst, function(x) +(ref %in% x$V1)))