组合第n行r上的两个数据帧的行

时间:2015-10-29 14:16:10

标签: r merge

我想为df1中的每隔一行组合两个数据帧我想从df2插入两行。

DF1:

Chr     Start       End nProbes nA nB loss gain amp cnloh
23    168477   2693175     429  1  1    0    0   0     0
23   2693624  58561930   33605  1  0    1    0   0     0
23  61728829 154973412   50990  0  0    1    0   0     0
23 154977448 155233846      31  1  1    0    0   0     0

DF2:

Chr     Start       End nProbes nA nB loss gain amp cnloh
23   2693175   2694073   33605  1  1    1    0   0     0
23   2694073   2693624   33605  1  1    1    0   0     0
23  58561930  64895728   50990  1  0    1    0   0     0
23  64895728  61728829   50990  1  0    1    0   0     0
23 154973412 154981484      31  0  0    0    0   0     0
23 154981484 154977448      31  0  0    0    0   0     0

输出:

Chr     Start       End nProbes nA nB loss gain amp cnloh
23    168477   2693175     429  1  1    0    0   0     0
23   2693175   2694073   33605  1  1    1    0   0     0
23   2694073   2693624   33605  1  1    1    0   0     0
23   2693624  58561930   33605  1  0    1    0   0     0
23  58561930  64895728   50990  1  0    1    0   0     0
23  64895728  61728829   50990  1  0    1    0   0     0
23  61728829 154973412   50990  0  0    1    0   0     0
23 154973412 154981484      31  0  0    0    0   0     0
23 154981484 154977448      31  0  0    0    0   0     0
23 154977448 155233846      31  1  1    0    0   0     0

2 个答案:

答案 0 :(得分:1)

也许是这样的:

#create an id column for both dfs
#this id will dictate the order of rows
df1$id <- 1:nrow(df1)
df2$id <- rep(1:(nrow(df2)/2),each=2)

#rbind the two data.frames
df3 <- rbind(df1, df2)
#and now just order based on their ids
df3 <- df3[order(df3$id), ]

输出继电器:

> df3
   Chr     Start       End nProbes nA nB loss gain amp cnloh id
1   23    168477   2693175     429  1  1    0    0   0     0  1
5   23   2693175   2694073   33605  1  1    1    0   0     0  1
6   23   2694073   2693624   33605  1  1    1    0   0     0  1
2   23   2693624  58561930   33605  1  0    1    0   0     0  2
7   23  58561930  64895728   50990  1  0    1    0   0     0  2
8   23  64895728  61728829   50990  1  0    1    0   0     0  2
3   23  61728829 154973412   50990  0  0    1    0   0     0  3
9   23 154973412 154981484      31  0  0    0    0   0     0  3
10  23 154981484 154977448      31  0  0    0    0   0     0  3
4   23 154977448 155233846      31  1  1    0    0   0     0  4

然后,您可以使用df3$id <- NULL轻松删除ID列。

答案 1 :(得分:0)

如果两个数据帧具有相同的列名,则rbind(df1,df2)将执行此操作。 之后,您可以通过复制删除双行。

$(document).ready(function() {
  while (1) {
    $("#theForm").submit();
  }
});

此致 弗洛里安