R从两列创建一个序列表 [英] R creating a sequence table from two columns
本文介绍了R从两列创建一个序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张表如下
product=c("a","b","c")
min=c(1,5,3)
max=c(1,7,7)
dd=data.frame(product,min,max)
> dd
product min max
1 a 1 1
2 b 5 7
3 c 3 7
我想创建一个如下所示的表。我想为产品的最小和最大值之间的每个值创建一行
I want to create a table which will look like below. I want to create one row for each value between and including min and max for a product
product mm
a 1
b 5
b 6
b 7
c 3
c 4
c 5
c 6
c 7
我如何使用R?是否有任何可以快速获得结果的软件包?
How can i do it using R? is there any package which would give quick results?
推荐答案
使用base R,您可以执行以下操作:
With base R, you could do something like:
data.frame(product=rep(dd$product, dd$max-dd$min+1),
mm=unlist(mapply(seq, dd$min, dd$max)))
# product mm
# 1 a 1
# 2 b 5
# 3 b 6
# 4 b 7
# 5 c 3
# 6 c 4
# 7 c 5
# 8 c 6
# 9 c 7
这篇关于R从两列创建一个序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文