user22364
user22364

Reputation: 47

converting pasted numbers to vector of numbers in r

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

Answers (3)

Wilson Freitas
Wilson Freitas

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

mrip
mrip

Reputation: 15163

Another option is to use gregexpr and regmatches:

as.integer(regmatches(t,gregexpr("\\d|-\\d",t))[[1]])

Upvotes: 3

Sven Hohenstein
Sven Hohenstein

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

Related Questions