Reputation: 47
I would like to convert the following string to separate values, including the minus sign
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"
to
(1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1, etc)
any advice is highly appreciated.
Upvotes: 2
Views: 90
Reputation: 531
Use stringr
's str_extract_all
function
as.integer(str_extract_all(t, '-?\\d')[[1]])
results
[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
check
> nchar(t)
[1] 149
> nchar(str_replace_all(t, '\\d', ''))
[1] 49
> length(as.integer(str_extract_all(t, '-?\\d')[[1]]))
[1] 100
Upvotes: 3
Reputation: 15163
Another option is to use gregexpr
and regmatches
:
as.integer(regmatches(t,gregexpr("\\d|-\\d",t))[[1]])
Upvotes: 3
Reputation: 81693
You can use strsplit
:
as.integer(strsplit(t, "(?<=\\d)", perl = TRUE)[[1]])
The result:
[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
[26] -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
[51] 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
[76] 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
Upvotes: 4