0

I have character vector of the following form (this is just a sample):

R1Ng(10)
test(0)
n.Ex1T(34)

where as can be seen above, the first part is always some combination of alphanumeric and punctuation marks, then there are parentheses with a number inside. I want to create a numeric vector which will store the values inside the parentheses, and each number should have name attribute, and the name attribute should be the string before the number. So, for example, I want to store 10, 0, 34, inside a numeric vector and their name attributes should be, R1Ng, test, n.Ex1T, respectively.

I can always do something like this to get the numbers and create a numeric vector:

counts <- regmatches(data, gregexpr("[[:digit:]]+", data))
as.numeric(unlist(counts))

But, how can I extract the first string part, and store it as the name attribute of that numberic array?

0

3 Answers 3

1

How about this:

x <- c("R1Ng(10)", "test(0)", "n.Ex1T(34)")

data.frame(Name = gsub( "\\(.*", "", x),
          Count = as.numeric(gsub(".*?\\((.*?)\\).*", "\\1", x)))

#     Name Count
# 1   R1Ng    10
# 2   test     0
# 3 n.Ex1T    34

Or alternatively as a vector

setNames(as.numeric(gsub(".*?\\((.*?)\\).*", "\\1", x)),
         gsub( "\\(.*", "", x ))
# R1Ng   test n.Ex1T 
# 10      0     34 
Sign up to request clarification or add additional context in comments.

Comments

0

Here is another variation using the same expression and capturing parentheses:

temp <- c("R1Ng(10)", "test(0)", "n.Ex1T(34)")

data.frame(Name=gsub("^(.*)\\((\\d+)\\)$", "\\1", temp),
           count=gsub("^(.*)\\((\\d+)\\)$", "\\2", temp))

Comments

0

We can use str_extract_all

library(stringr)
lst <- str_extract_all(x, "[^()]+")

Or with strsplit from base R

lst <- strsplit(x, "[()]")

If we need to store as a named vector

sapply(lst, function(x) setNames(as.numeric(x[2]), x[1]))
#  R1Ng   test n.Ex1T 
#   10      0     34 

data

x <- c("R1Ng(10)", "test(0)", "n.Ex1T(34)")

Comments