How to append group row into dataframe

后端 未结 9 2003
借酒劲吻你
借酒劲吻你 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
      <int> <int> <int>
    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))
    
    0 讨论(0)
  • 2021-02-07 11:32

    We can use base R methods

    rbind(df1, setNames(as.data.frame(do.call(cbind, rep(list(df2$A), 3))), names(df1)))
    #  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))
    
    0 讨论(0)
  • 2021-02-07 11:33
    data.frame(sapply(df1, c, unlist(df2)), row.names = NULL)
    #  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))
    
    0 讨论(0)
  • 2021-02-07 11:33

    I just love R, here is yet another Base R solution but with mapply:

    data.frame(mapply(c, 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
    

    Note:

    No need to deal with colnames like almost all the other solutions... The key to why this works is that "mapply calls FUN for the values of ... [each element] (re-cycled to the length of the longest...[element]" (See ?mapply). In other words, df2$A is recycled to however many columns df1 has.

    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", row.names = c(NA, -4L), class = "data.frame")
    
    0 讨论(0)
  • 2021-02-07 11:39

    We can replicate df2 for the number of columns of df1, unname it, then rbind it.

    rbind(df1, unname(rep(df2, ncol(df1))))
    #   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", row.names = c(NA, -4L), class = "data.frame")
    
    0 讨论(0)
  • 2021-02-07 11:40

    Here is a base R method with rbind, rep, and setNames:

    rbind(dat, setNames(data.frame(rep(dat1, ncol(dat))), names(dat)))
      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
    

    Edit: turns outdata.frame isn't necessary:

    rbind(dat, setNames(rep(dat1, ncol(dat)), names(dat)))
    

    will work.

    data

    dat <- 
    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))
    
    dat1 <-
    structure(list(A = 1:4), .Names = "A", row.names = c(NA, -4L),
    class = "data.frame")
    
    0 讨论(0)
提交回复
热议问题