[R] assign() and paste() for data.frame() in nested for loops

jssdenton jdenton at amnh.org
Tue Apr 24 22:17:37 CEST 2012


Hi folks,

I am trying to recurse through several subfolders in a directory. Each
folder has many entries that I want to display summary values for. I have
been expanding data frames using code with the structure name <- rbind(name,
newvals) to produce a data frame with n rows equal to the number of files in
one of the folders, and n column equal to the number of values in the file.

I can loop over the values within a single subdirectory fine with, for
example, 

trees <- list.files(pattern="*.tre")
iters=length(trees)

branchdata.5 <- data.frame()

iterations <- as.character(c(1:length(trees)))

for (i in 1:iters) {

tree <- read.tree(trees[i])
iteration.edges.5 <- as.numeric(tree$edge.length)

branchdata.5 <- rbind(branchdata.5, iteration.edges.5)

} 

The problem comes when I want to iterate through the subdirectories while
also iterating through the files in a given directory. I want to recursively
assign these data frames as well, with something like

f <- list.dirs(path = "/.../.../etc", full.names = FALSE, recursive = FALSE)

for (j in 1:length(f)) {

setwd(paste("/.../.../.",j,sep=""))

assign( paste("branchdata.5",j,sep=""), data.frame() )

iterations <- as.character(c(1:length(trees)))

for (i in 1:iters) {

tree <- read.tree(trees[i])
assign(paste("iteration.edges.5",j,sep=""), as.numeric(tree$edge.length) )

paste("branchdata.5",j,sep="") <- rbind(paste("branchdata.5",j,sep=""),
paste("iteration.edges.5",j,sep=""))

}

names(iterations) <- NULL
boxplot(t(paste("branchdata.5",j,sep="")) , horizontal=TRUE ,
names=iterations , ylim=c(0,2), xlab="Branch Lengths" , ylab="Iterations" ,
main = "GTR + indels")

}

The problem seems to be in the rbind() when using values with assign() and
paste(). I would love some help on this!

--
View this message in context: http://r.789695.n4.nabble.com/assign-and-paste-for-data-frame-in-nested-for-loops-tp4584692p4584692.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list