代码之家  ›  专栏  ›  技术社区  ›  Tom

添加列项目,作为另一列中列表的第一个元素

  •  1
  • Tom  · 技术社区  · 3 年前

    我的数据如下:

    dat <- structure(list(`[0,25)` = c(5L, 0L), freq = list(c(43, 20, 38, 
    27, 44, 177), c(5, 3, 12, 53, 73))), class = c("rowwise_df", 
    "tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L), groups = structure(list(
        .rows = structure(list(1L, 2L), ptype = integer(0), class = c("vctrs_list_of", 
        "vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df", 
    "tbl", "data.frame")))
    

    enter image description here

    我只想在列中添加这个项目 [0,25) (作为第一项)添加到列中的列表中 freq .

    期望输出

    dat <- structure(list(`[0,25)` = c(5L, 0L), freq = list(c(5, 43, 20, 38, 
    27, 44, 177), c(0, 5, 3, 12, 53, 73))), class = c("rowwise_df", 
    "tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L), groups = structure(list(
        .rows = structure(list(1L, 2L), ptype = integer(0), class = c("vctrs_list_of", 
        "vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df", 
    "tbl", "data.frame")))
    

    enter image description here

    我试过:

    dat$freq <- lapply(dat$freq, \(x){
      x <- append(dat$`[0,25)`, x)
      x
    })
    

    但这是整个向量的附加。我该怎么做?

    1 回复  |  直到 3 年前
        1
  •  1
  •   jay.sf    3 年前

    c 一次约会。

    transform(dat, freq=Map('c', dat[[1]], dat[[2]]))
    #   X.0.25.                       freq
    # 1       5 5, 43, 20, 38, 27, 44, 177
    # 2       0        0, 5, 3, 12, 53, 73