How to append group row into dataframe

后端 未结 9 2016
借酒劲吻你
借酒劲吻你 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:28

    A solution from purrr, which uses map_dfc to loop through all columns in df1 to combine all the elements with df2$A.

    library(purrr)
    
    map_dfc(df1, ~c(., df2$A))
    
    # A tibble: 6 x 3
          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
    

    Data

    df1 <- structure(list(A = c(1L, 5L), B = c(2L, 7L), C = c(3L, 9L)), .Names = c("A", 
                                                                                   "B", "C"), class = "data.frame", row.names = c(NA, -2L))
    
    df2 <- structure(list(A = 1:4), .Names = "A", class = "data.frame",
                     row.names = c(NA, -4L))
    

提交回复
热议问题