Combine (rbind) data frames and create column with name of original data frames

后端 未结 6 1658
栀梦
栀梦 2020-11-22 13:12

I have several data frames that I want to combine by row. In the resulting single data frame, I want to create a new variable identifying which data set the observation came

6条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 13:45

    Another workaround for this one is using ldply in the plyr package...

    df1 <- data.frame(x = c(1,3), y = c(2,4))
    df2 <- data.frame(x = c(5,7), y = c(6,8))
    list = list(df1 = df1, df2 = df2)
    df3 <- ldply(list)
    
    df3
      .id x y
      df1 1 2
      df1 3 4
      df2 5 6
      df2 7 8
    

提交回复
热议问题