How to append group row into dataframe

后端 未结 9 2225
借酒劲吻你
借酒劲吻你 2021-02-07 10:55

I have this df1:

A B C
1 2 3
5 7 9

where A B C are columns names.

I have another df2 with one column:

A
1
         


        
9条回答
  •  眼角桃花
    2021-02-07 11:40

    Data:

    df1 <- data.frame(A=c(1,5),
                      B=c(2,7),
                      C=c(3,9))
    df2 <- data.frame(A=c(1,2,3,4))
    

    Solution:

    df2 <- matrix(rep(df2$A, ncol(df1)), ncol=ncol(df1))
    colnames(df2) <- colnames(df1)
    rbind(df1,df2)
    

    Result:

      A B C
    1 1 2 3
    2 5 7 9
    3 1 1 1
    4 2 2 2
    5 3 3 3
    6 4 4 4
    

提交回复
热议问题