我想将以下字符串转换为单独的值,包括减号
t <- "111111111-1-1-1-1-1-1-1-11111-1-1-1-1-1111111-1-1-1-1-1-1-1-111111-1-1-1-111111-1-1-1-1-1-1-1-111111-1-1-1-1111111-1-1-1-1-1-1-1-1111-1-1-1-111111111"
到
(1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1, etc)
任何建议都受到高度赞赏。
使用stringr
的str_extract_all
功能
as.integer(str_extract_all(t, '-?\\d')[[1]])
结果
[1] 1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1
[42] 1 1 1 1 -1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 1 1 1 1 1 -1 -1 -1 -1 -1
[83] -1 -1 -1 1 1 1 -1 -1 -1 -1 1 1 1 1 1 1 1 1
查看
> nchar(t)
[1] 149
> nchar(str_replace_all(t, '\\d', ''))
[1] 49
> length(as.integer(str_extract_all(t, '-?\\d')[[1]]))
[1] 100
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句