Reputation: 349
I am running a loop across thousands of file to conduct a Pagel Lambda
files <- list.files(pattern=".txt")
column_names <- data.frame(files = "files", V1 = "V1", Lambda = "lamda", LogL = "logL", LogL10 = "logL10", pvalue = "P")
write.table(column_names, file = "output.csv", row.names = FALSE,
append = FALSE, col.names = FALSE, sep = ", ", quote = TRUE)
for(i in files){
graph <- read.table(i,header=F)
row.names(graph)<-graph[,1]
trait<-graph[,-1]
names(trait)<-row.names(graph)
result <- data.frame(t(c(files[i], phylosig(mytree, trait, method="lambda", test=TRUE, nsim=999))))
write.table(result, file = "output.csv", row.names = FALSE,
append = TRUE, col.names = FALSE, sep = ", ")
}
My goal is to get one output file with 1 line for each input file's result. My variable result looks like this:
[1] "some data in x given as 'NA', dropping corresponding species from tree"
V1 lambda logL logL0 P
1 EOG7B0F7J.txt 6.610696e-05 -59.62533 -59.62509 1
I need these values to be in the output file AND the name of the input file (I did not set that yet in my loop, I have no idea how to do it)
So I would like the output to look like this:
FileA V1 lambda logL logL0 P
FileB V1 lambda logL logL0 P
FileC V1 lambda logL logL0 P
Or something similar - I would be super happy if somebody could help me out with this
Thanks a lot
Upvotes: 1
Views: 96
Reputation: 349
I figured it out, here is the answer if somebody is interested:
column_names <- data.frame(i = "files", Lambda = "lamda", LogL = "logL", LogL10 = "logL10", pvalue = "P")
write.table(column_names, file = "output.csv", row.names = FALSE,
append = FALSE, col.names = FALSE, sep = ", ", quote = TRUE)
for(i in files){
graph <- read.table(i,header=F)
row.names(graph)<-graph[,1]
trait<-graph[,-1]
names(trait)<-row.names(graph)
result <- data.frame(t(c(i, phylosig(mytree, trait, method="lambda", test=TRUE, nsim=999))))
my.df <- data.frame(lapply(result, as.character), stringsAsFactors=FALSE)
write.table(my.df, file = "output.csv", row.names = FALSE, append = TRUE, col.names = FALSE, sep = ", ")
}
Upvotes: 1