我有一列数据字符串看起来像这样 1402 2010 10
我想用下划线替换第一个空格,并用正斜杠替换第二个空格,以便看起来像这样 1402_2010/10
使用,gsubfn
您可以编写一个跟踪比赛索引的函数count
,例如1代表第一次比赛,2代表第二次比赛,等等,并相应地更改替换项
gsubfn(' ', proto(fun = function(this, x) c('_', '/')[count]), v)
#[1] "1402_2010/10"
或者,使用Reduce使用base R实现相同的想法
seq_sub <- function(x, pattern, replacement){
Reduce(function(x, y) sub(pattern, y, x), replacement, init = x)
}
seq_sub('some words here ', ' ', c(9, 5, 2))
# [1] "some9words5here2"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句