我有一个数据帧作为多的行,两列的范围如下:扩展的数据帧具有在原始行
structure(list(symbol = c("u", "n", "v", "i", "a"), start = c(9L,
6L, 10L, 8L, 7L), end = c(14L, 15L, 12L, 13L, 11L)), .Names = c("symbol",
"start", "end"), class = "data.frame", row.names = c("1", "2",
"3", "4", "5"))
我希望尽可能多的行,也有在的范围内的值(开始,结束)每个符号。所以,最后的数据帧的样子:
structure(list(symbol = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("a", "l", "n", "v", "y"
), class = "factor"), value = c(7L, 8L, 9L, 10L, 11L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 8L, 9L, 10L, 11L, 12L, 10L,
11L, 12L, 13L, 14L, 15L, 9L, 10L, 11L, 12L, 13L)), class = "data.frame", row.names = c(NA,
-30L), .Names = c("symbol", "value"))
我想我可以简单地每行值的列表,然后使用tidyr
包的unnest
如下:
df$value <- apply(df, 1, function(x) as.list(x[2]:x[3]))
dput(df)
structure(list(symbol = structure(c(4L, 3L, 5L, 2L, 1L), .Label = c("a",
"i", "n", "u", "v"), class = "factor"), start = c(9L, 6L, 10L,
8L, 7L), end = c(14L, 15L, 12L, 13L, 11L), value = structure(list(
`1` = list(9L, 10L, 11L, 12L, 13L, 14L), `2` = list(6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L), `3` = list(10L,
11L, 12L), `4` = list(8L, 9L, 10L, 11L, 12L, 13L), `5` = list(
7L, 8L, 9L, 10L, 11L)), .Names = c("1", "2", "3", "4",
"5"))), .Names = c("symbol", "start", "end", "value"), row.names = c("1",
"2", "3", "4", "5"), class = "data.frame")
df
symbol start end value
1 u 9 14 9, 10, 11, 12, 13, 14
2 n 6 15 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
3 v 10 12 10, 11, 12
4 i 8 13 8, 9, 10, 11, 12, 13
5 a 7 11 7, 8, 9, 10, 11
然后做:
library(tidyr)
unnest(df, value)
不过,我觉得我打这个悬而未决的特性/错误: https://github.com/tidyverse/tidyr/issues/278
Error: Each column must either be a list of vectors or a list of data frames [value]
有没有更好的方法来做到这一点,特别是避免申请家庭?
织补简单,呵呵!我只是一直忘记'做'具有多大的力量。试图玩一点这个问题,但只是不能提出正确的步骤。完善。谢谢! – Gopala