问题描述
以下列表是使用st_intersects()
包中的sf
的结果。它包含数值向量,长度随长度而变化:
require(sf)
require(tidyverse)
mylist <- structure(list(c(1L,71L,168L,672L),c(1L,99L,619L),c(3L,47L,51L),c(32L,43L,76L,78L,318L,411L),c(4L,143L,344L,363L,724L),17L,251L,327L,330L,450L,c(44L,75L,79L,124L,172L,204L,339L,350L,382L,383L,404L,405L,409L,540L,554L,562L,605L,623L,661L,689L),c(405L,605L),c(124L,249L,661L),c(8L,13L,175L,187L)),predicate = "intersects",region.id = c("1","2","3","4","5","6","7","8","9","10"),ncol = 825L,class = c("sgbp","list"))
也就是说,我想获得一个看起来像
的对象ID V1
1 1L
1 71L
1 168L
1 672L
2 1L
2 71L
2 99L
2 619L
...
我将如何实现上述目标,最好在dplyr
中实现?
解决方法
您可以先使用enframe
,然后再使用unnest
:
tibble::enframe(df,name = 'ID',value = 'V1') %>% tidyr::unnest(V1)
# A tibble: 63 x 2
# ID V1
# <int> <int>
# 1 1 1
# 2 1 71
# 3 1 168
# 4 1 672
# 5 2 1
# 6 2 71
# 7 2 99
# 8 2 619
# 9 3 3
#10 3 47
# … with 53 more rows
在基数R中,您可以使用Map
:
do.call(rbind.data.frame,Map(cbind,ID = seq_along(df),V1 = df))
,
带有base
的{{1}}解决方案:
stack()