我试图实现:
df[, sprintf("mid_o_lag_%0d", 1:20) := shift(mid_o, c(1:20), type = 'lag')]
df[, sprintf("mid_d_lag_%0d", 1:20) := shift(mid_d, c(1:20), type = 'lag')]
df[, sprintf("mid_o_lead_%0d", 1:20) := shift(mid_o, c(1:20), type = 'lead')]
df[, sprintf("mid_d_lead_%0d", 1:20) := shift(mid_d, c(1:20), type = 'lead')]
可以这样做:
'abc'.scan(regex) #=> ['a', 'b', 'c', 'ab', 'bc', 'abc']
但我想用一个正则表达式来表达。
答案 0 :(得分:3)
没有正则表达式怎么样?:
string = 'abc'
p (1..string.size).flat_map { |e| string.chars.each_cons(e).map(&:join) }
# ["a", "b", "c", "ab", "bc", "abc"]