ToNoY
ToNoY

Reputation: 1378

Joining data by 'rbind' as a loop in R

I have two equally long dataset - 'vpXmin' and 'vpXmax' created from 'vp'

> head(vpXmin)
                 vp
[1,] 253641 2621722
[2,] 253641 2622722
[3,] 253641 2623722
[4,] 253641 2624722
[5,] 253641 2625722
[6,] 253641 2626722

> head(vpXmax)
                 vp
[1,] 268641 2621722
[2,] 268641 2622722
[3,] 268641 2623722
[4,] 268641 2624722
[5,] 268641 2625722
[6,] 268641 2626722

I want to join each of the rows from these datasets using 'rbind' and want to create separate matrix; e.g.

l1<-rbind(vpXmax[1,],vpXmin[1,])
l2<-rbind(vpXmax[2,],vpXmin[2,])
 ... ...

Even though I'm not familiar with R loops, I want to deal with such a large data as a loop ... but I failed while trying this:

for (i in 1:length(vp)){rbind(vpXmax[i,],vpXmin[i,])}

Any idea why? Also, please gimme some good references for learning different kinds of loops using R, if any. thanks in advance.

Upvotes: 2

Views: 9490

Answers (2)

thelatemail
thelatemail

Reputation: 93843

Maybe something like:

vpXmax <- matrix(1:10,ncol=2)
vpXmin <- matrix(11:20,ncol=2)

l <- lapply(1:nrow(vpXmin),function(i) rbind(vpXmax[i,],vpXmin[i,]) )

Then, instead of l1, l2 etc etc you have

l[[1]]
#     [,1] [,2]
#[1,]    1    6
#[2,]   11   16
l[[2]]
#     [,1] [,2]
#[1,]    2    7
#[2,]   12   17

And although it is probably not ideal, there is one major thing wrong with your initial loop. You aren't assigning your output, so you need to use assign or <- in some way to actually make an object. However, using assign, is pretty much a flag to set off alarm bells that there is a better way to do things, and <- would require pre-allocating or other stuffing around. Nevertheless, it will work, albeit polluting your work space with l1 l2... ln objects:

for (i in 1:nrow(vpXmax)) {assign(paste0("l",i), rbind(vpXmax[i,],vpXmin[i,]) )}

> l1
#     [,1] [,2]
#[1,]    1    6
#[2,]   11   16

> l2
#     [,1] [,2]
#[1,]    2    7
#[2,]   12   17

Upvotes: 0

griverorz
griverorz

Reputation: 697

As @ToNoy indicates, it is not obvious the kind of output that you want. The easiest way to proceed would be to create a list in which each element is the result of rbind each row of the two original data frames.

A <- data.frame("a" = runif(100, -1, 0), "b" = runif(100, 0, 1))                                                                                                                     
Z <- data.frame("a" = runif(100, -2, -1), "b" = runif(100, 1, 2))                                                                                                                    

output <- vector("list", nrow(A))                                                                                                                                                    
for (i in 1:nrow(A)) {                                                                                                                                                               
    output[[i]] <- rbind(A[i, ], Z[i, ])                                                                                                                                             
}                                                                                             

Upvotes: 0

Related Questions