给定以下数据框:
df <- data.frame(start = c("005", "010", "014"),
end = c("005", "013", "017"),
zone = c(3, 5, 7))
# df
# start end zone
# 1 005 005 3
# 2 010 013 5
# 3 014 017 7
我想生成以下结果:
# key zone
# 1 005 3
# 2 010 5
# 3 011 5
# 4 012 5
# 5 013 5
# 6 014 7
# 7 015 7
# 8 016 7
# 9 017 7
我在想我也许可以利用tidyr
- 也许complete()
或的东西expand()
,但是有三个字符的字符串,df$start
并且df$end
一直给我带来麻烦。
我使用: 取得了一定的成功apply(df, 1, function(i) seq(as.numeric(i["start"]), as.numeric(i["end"])))
,然后我可以将其传递给类似的东西stringr::str_pad(..., width = 3, pad = "0")
,但我不确定如何巧妙地抓住这些重复序列的区域。