如何将一个数据帧的单行添加到另一数据帧?

时间:2019-08-02 03:18:07

标签: r

我有2个数据框,它们的列数相同但行数不同。 Df 1具有从时间点1开始的ID的行,而Df 2应该具有从时间点2开始的相同ID的行。Df 1应该仅具有时间点1的数据,但是具有来自时间点2的数据,所以我想将Df1中具有时间点2数据的单行传输到Df2。

我已经使用ID ##这样的值提取了值<-T1 [148],但是找不到将提取的值添加到Df2底部的新行中的方法。

1 个答案:

答案 0 :(得分:0)

多种方法:

  1. base(由@Gregor指出):
 Df2 <- rbind(Df2, Df1[Df1$timepoint == 2, ]
  1. 使用dplyrtidyverse):
      Df2 %>%
          rbind(Df1 %>%
          filter(timepoint == 2)) %>% # Add all rows from Df1 fitting the condition
          {.} -> Df2 # To save the changed Df2
  1. 使用dplyrtidyverse)和bind_rows(由@MrGumble指出):
      Df2 %>%
          bind_rows(Df1 %>%
          filter(timepoint == 2)) %>% # Add all rows from Df1 fitting the condition
          {.} -> Df2 # To save the changed Df2