问题描述
使用reshape2
,很容易将向量的深层嵌套列表转换为长的data.frame,其中包含有关每个值的原始列表位置的信息。
# generate nested list of depth 3 with 2 branches at each level
n_l <- purrr::array_tree(
array(1:2^3,rep_len(2,3))
)
m_n_l <- reshape2::melt(n_l)
# this provides a df of values where original list position is labelled using
# [n_dimensions] columns each with [n_branches] values. yay! that's what I want
m_n_l
# value L3 L2 L1
# 1 1 1 1 1
# 2 5 2 1 1
# 3 3 1 2 1
# 4 7 2 2 1
# 5 2 1 1 2
# ...
# [reshape2::melt() also handles the case where leaf node vectors have
# arbitrary number of elements]
reshape2::melt(rapply(n_l,function(x) x * 1:sample(1:3,1)))
reshape2
现在已经退休,并且鼓励其用户使用tidyr
。但是,我找不到一种tidyr
的方法来复制reshape2::melt()
的上述功能。 pivot_longer()
,hoist()
和unnest()
似乎是取代melt()
的函数,但是它们似乎专用于data.frame或data.frame列表的特定情况。
tidyr
可以这样做吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)