本文介绍了在R中的rbind命令之后创建一个标识原始data.frame的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R还是比较陌生,我想知道如何在使用rbind命令加入连接之前创建一个变量(数字序列)以标识每个原始data.frames.

I am relatively new to R and I would like to know how can I create a variable (number sequence) that identifies the each of the original data.frames before being joined with the rbind command.

由于在原始数据帧中有一个变量是行ID号,因此,如果创建一个循环,每次遇到行ID中的数字1时都会在新变量中分配一个新数字,则该循环应该起作用.

Since in the original data frames there is one variable that is a row ID number, if creating a loop that assigns a new number in the new variable each time it encounters the number 1 in the row ID, it should work...

谢谢.

推荐答案

gdata程序包中有一个名为combine的函数可以做到这一点.

There's a function in the gdata package called combine that does just that.

df1 <- data.frame(a = seq(1, 5, by = 1),
                  b = seq(21, 25, by = 1))

df2 <- data.frame(a = seq(6, 10, by = 1),
                  b = seq(26, 30, by = 1))

library(gdata)
combine(df1, df2)

    a  b source
1   1 21    df1
2   2 22    df1
3   3 23    df1
4   4 24    df1
5   5 25    df1
6   6 26    df2
7   7 27    df2
8   8 28    df2
9   9 29    df2
10 10 30    df2

这篇关于在R中的rbind命令之后创建一个标识原始data.frame的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:35