R: subset a matrix of unknown length using a for loop -


i using values in 1 matrix (data1) determine how subset second matrix (data2) smaller matrix (foo). running problems have no way of knowing ahead of time size of foo.

here mwe (the actual data structs in question 100,000+ rows long).

data1 <- rbind(c(102,250,'stim1'),c(477,839,'stim2')) data2 <- rbind(c(99,'xx'),c(105,'yy'),c(230,'zz'),c(312,'aa'),c(587,'bb'),c(846,'cc'))  foo <- null for(i in 1:nrow(data1)) {            foo[i,1] <- subset(data2,as.numeric(data2[,1]) > as.numeric(data1[i,1]) & as.numeric(data2[,1]) < as.numeric(data1[i,2]))         foo[i,2] <- rep.int(data1[i,3],nrow(subset(data2,as.numeric(data2[,1]) > as.numeric(data1[i,1]) & as.numeric(data2[,1]) < as.numeric(data1[i,2])))) } 

i foo this:

105 yy stim1 230 zz stim1 587 bb stim2 

first, put have in data.frame.

data1 <- data.frame(a=c(102, 477), b=c(250, 839), c=c("stim1", "stim2")) data2 <- data.frame(a=c(99, 105, 230, 312, 587, 846),                       b=c("xx", "yy", "zz", "aa", "bb", "cc")) 

then can use apply:

list <- apply(data1, 1, function(x)                       cbind(data2, c=x[[3]])[data2$a > x[1] & data2$a < x[2],]) 

then rbind list:

df <- do.call(rbind, list) df       b     c 2 105 yy stim1 3 230 zz stim1 5 587 bb stim2 

Comments