我的資料如下:
dat <- structure(
list(
freq = list(a= c(5, 38, 43, 27, 44, 20, 177), b=c(3, 5, 12, 53, 73))),
row.names = c("A", "B"), class = "data.frame")
我唯一想做的就是得到一個包含串列名稱的列,以及一個包含串列的列:
data.frame:
rn values
1. A c(5, 38, 43, 27, 44, 20, 177)
2. B c(3, 5, 12, 53, 73)
但我就是想不通..
這應該怎么做?
uj5u.com熱心網友回復:
使用names:
dat$rn <- names(dat$freq)
dat
# freq rn
#A 5, 38, 43, 27, 44, 20, 177 a
#B 3, 5, 12, 53, 73 b
您還可以使用rownames_to_column:
tibble::rownames_to_column(dat, "rn")
# rn freq
#1 A 5, 38, 43, 27, 44, 20, 177
#2 B 3, 5, 12, 53, 73
uj5u.com熱心網友回復:
我們可以用imap
library(purrr)
imap_dfr(dat$freq, ~ tibble(rn = toupper(.y), values = list(.x)))
-輸出
# A tibble: 2 × 2
rn values
<chr> <list>
1 A <dbl [7]>
2 B <dbl [5]>
或與enframe
library(tibble)
enframe(dat$freq)
# A tibble: 2 × 2
name value
<chr> <list>
1 a <dbl [7]>
2 b <dbl [5]>
uj5u.com熱心網友回復:
rownames_to_column()in的替代方法tibble是設定 的arg rownames。as_tibble()
tibble::as_tibble(dat, rownames = "rn")
# A tibble: 2 × 2
rn freq
<chr> <named list>
1 A <dbl [7]>
2 B <dbl [5]>
uj5u.com熱心網友回復:
采用cbind
cbind(rn=rownames(dat), dat[1])
# rn freq
# A A 5, 38, 43, 27, 44, 20, 177
# B B 3, 5, 12, 53, 73
順便說一句,這是您最初問題的完整解決方案。
res <- apply(dat_in[-c(1, length(dat_in))], 1, \(x) list2DF(list(unname(x[x != 0])))) |>
list2DF() |> t() |> as.data.frame() |> cbind(dat_in$rn) |> subset(select=2:1) |>
setNames(c('rn', 'values'))
res
# rn values
# 1 W 5, 38, 43, 27, 44, 20
# 2 M 3, 5, 12, 53
在哪里
str(res)
# 'data.frame': 2 obs. of 2 variables:
# $ rn : chr "W" "M"
# $ values:List of 2
# ..$ 1: int 5 38 43 27 44 20
# ..$ 2: int 3 5 12 53
資料:
dat_in <- structure(list(rn = c("W", "M"), ` 0` = c(0L, 0L), `[ 0, 25)` = c(5L,
0L), `[ 25, 50)` = c(0L, 0L), `[ 25, 100)` = c(38L,
3L), `[ 50, 100)` = c(0L, 0L), `[ 100, 250)` = c(43L,
5L), `[ 100, 500)` = c(0L, 0L), `[ 250, 500)` = c(27L,
12L), `[ 500, 1000)` = c(44L, 0L), `[ 500,1000000]` = c(0L,
53L), `[ 1000, 1500)` = c(0L, 0L), `[ 1000,1000000]` = c(20L,
0L), `[ 1500, 3000)` = c(0L, 0L), `[ 3000,1000000]` = c(0L,
0L), Sum_col = c(177, 73)), row.names = 1:2, class = c("data.table",
"data.frame"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/462268.html
