在R中创建嵌套序列

时间:2015-07-29 09:11:42

标签: r sequence paste

我想创建以下vector,其中包含两个嵌套的sequences,以及字母ab

desired.data <- c('a1b1', 'a1b2', 'a1b3', 'a2b1','a2b2', 'a2b3', 
                  'a3b1', 'a3b2', 'a3b3', 'a4b1','a4b2', 'a4b3', 
                  'a5b1', 'a5b2', 'a5b3')

我怀疑这是重复的,但我搜索了Stack Overflow一个小时没有成功。感谢您的任何建议。

2 个答案:

答案 0 :(得分:7)

使用Select o.Id as OrderId, ol.ProductId, ol.Amount from Orders o inner join OrderLines ol on o.Id = ol.OrderId where o.Id in (Select top 3 OrderId from Orders where OwnerId = @OwnerId) Order By o.OrderDate desc paste0及其rep参数,并依赖于矢量回收:

each

答案 1 :(得分:3)

如果字符串中的模式比仅仅两个数字和两个字符更复杂,这里有一个替代解决方案可能更可行

concat <- function(x) paste0('a', x[, 2], 'b', x[, 1])
concat(expand.grid(1:3, 1:5))
#[1] "a1b1" "a1b2" "a1b3" "a2b1" "a2b2" "a2b3" "a3b1" "a3b2" "a3b3" "a4b1" "a4b2" "a4b3" "a5b1" "a5b2" "a5b3"