简体   繁体   中英

Insert row every nth value from one dataframe to another in R

I am trying to insert many rows from one dataframe to another. I managed to do it once, but I have to do the same 3500 times.

I have the following two dataframes with the same headers:

dataframe a with 850561 rows and 121 columns

dataframe b with 854001 rows and 121 columns

And I used the following code to insert a row from b to a:

a <- rbind(a[1:244,],b[245],a[-(1:244),])

This works perfectly because it inserts a row from b in between rows 244 and 245 of a.

The problem is that I have to do the same every 243 rows, for example the next would be something like this:

a <- rbind(a[246:489,],b[489],a[-(246:489),])

I have tried with the following for loop, but it does not work:

i<-244
j<-1
for (val in a){
  a<-rbind(a[j:i,],b[i+1],a[-(j:i),])
  i<-i+243
  j<j+245
}

I would much appreciate your help

One possible approach is to rbind(a,b) into a single table, with a new variable which when sorted on gives rows in the correct order.

This new variable would take the values 1:(dim(a)[1]) for the existing rows of a, then 245.5, 489.5, etc for the rows of b . Then if I sort on it the rows of b will appear in the correct place.

(Edit: I've just realised this is slightly different to your situation, but it should be easily adaptable, all you would need to do is select the appropriate rows of b first)

Here's a toy example:

# Create a and b
a <- data.frame(x=runif(15),source="a")
b <- data.frame(x=runif(3),source="b")

# Suppose I want b interleaved with a, such that a row of b occurs every 5 rows of a:

a$sortorder <- 1:dim(a)[1]
b$sortorder <- (1:dim(b)[1])*5+0.5

dat <- rbind(a,b)
dat[order(dat$sortorder),-3]

            x source
1  0.10753897      a
2  0.24482683      a
3  0.72236241      a
4  0.03273494      a
5  0.54934999      a
16 0.20578103      b
6  0.68280868      a
7  0.30024090      a
8  0.38877446      a
9  0.73244615      a
10 0.96249642      a
17 0.13878037      b
11 0.76059617      a
12 0.58167316      a
13 0.46287299      a
14 0.35630962      a
15 0.38392182      a
18 0.38908214      b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM