[R] indexing names for looping across computations done on pairs of matrices

arun smartpink111 at yahoo.com
Wed Apr 16 04:11:11 CEST 2014


If the two pairs of matrices are in a list:
lst1 <- lapply(1:11, function(x) matrix(sample(40, 20, replace=TRUE), 5,4)) 

names(lst1) <- paste0("gs", paste0("4.",seq(0,100,by=10)))

lst2 <- lapply(1:11, function(x) matrix(sample(40, 20, replace=TRUE), 5,4)) 

names(lst2) <- paste0("ps", paste0("1.",seq(0,100,by=10)))
mean.comb <- data.frame(simulation=0:10, mean.horses.removed=sapply(seq_along(lst1),function(i) mean(rowSums(lst1[[i]]*lst2[[i]])))) 


# if the datasets are standalone with no particular order
#just for demonstration


vec1 <- sample(paste0("gs", paste0("4.",seq(0,100,by=10))), 11, replace=FALSE)
vec2 <- sample(paste0("ps", paste0("1.",seq(0,100,by=10))), 11, replace=FALSE) 

vec1New <- vec1[order(as.numeric(gsub(".*\\.","",vec1)))] 

vec2New <- vec2[order(as.numeric(gsub(".*\\.","",vec2)))] 

mean.comb2 <- data.frame(simulation=0:10, mean.horses.removed=sapply(seq_along(vec1New),function(i) mean(rowSums(get(vec1New[i])* get(vec2New[i])))))

#[1] TRUE 


On Tuesday, April 15, 2014 4:30 PM, "Cade, Brian" <cadeb at usgs.gov> wrote:
So I know I must be missing something simple and obvious for the following
data manipulation where I have (in this example) 11 pairs of matrices
(gs4.0 to gs4.100 and ps1.0 to ps1.100) from some population simulations
(all with same dimensions) where I want to get some summary statistics on
the products of the cells in a pair (e.g., gs4.0 * ps1.0).  The code I
wrote below works fine, but it seems like there ought to be a simple way to
index the extensions on the names (.0 to .100) in a for loop to simplify
this code greatly.  I've spent some time trying various things using
paste() and assign() and have had no success.

mean.comb <- as.matrix(0:10,nrow = 11, ncol=1)
mean.comb <- cbind(mean.comb,0)

###to see list of files created
gs4files <- ls(pattern="gs4.*0")
ps1files <- ls(pattern="ps1.*0")

mean.comb[1,2] <- mean(apply(gs4.0 * ps1.0,1,sum))
mean.comb[2,2] <- mean(apply(gs4.10 * ps1.10,1,sum))
mean.comb[3,2] <- mean(apply(gs4.20 * ps1.20,1,sum))
mean.comb[4,2] <- mean(apply(gs4.30 * ps1.30,1,sum))
mean.comb[5,2] <- mean(apply(gs4.40 * ps1.40,1,sum))
mean.comb[6,2] <- mean(apply(gs4.50 * ps1.50,1,sum))
mean.comb[7,2] <- mean(apply(gs4.60 * ps1.60,1,sum))
mean.comb[8,2] <- mean(apply(gs4.70 * ps1.70,1,sum))
mean.comb[9,2] <- mean(apply(gs4.80 * ps1.80,1,sum))
mean.comb[10,2] <- mean(apply(gs4.90 * ps1.90,1,sum))
mean.comb[11,2] <- mean(apply(gs4.100 * ps1.100,1,sum))

mean.comb<- data.frame(mean.comb)
colnames(mean.comb) <- c("simulation", "mean.horses.removed")


Brian S. Cade, PhD

U. S. Geological Survey
Fort Collins Science Center
2150 Centre Ave., Bldg. C
Fort Collins, CO  80526-8818

email:  cadeb at usgs.gov <brian_cade at usgs.gov>
tel:  970 226-9326

    [[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list