添加列项,作为列表中另一列的第一个元素
Add a column item, as the first element of a list in another column
我有如下数据:
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")))
我只想将 [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")))
我试过了:
dat$freq <- lapply(dat$freq, \(x){
x <- append(dat$`[0,25)`, x)
x
})
但这会附加整个向量。我应该怎么做?
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
我有如下数据:
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")))
我只想将 [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")))
我试过了:
dat$freq <- lapply(dat$freq, \(x){
x <- append(dat$`[0,25)`, x)
x
})
但这会附加整个向量。我应该怎么做?
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