Home > OS >  Creating a numeric array out of string
Creating a numeric array out of string

Time:11-11

I am trying to create a numeric array out of two columns of a data.frame. I have the columns "first" and "last" and I create the "range" one with paste0(first,':',last).

I get to the point where I have the string with which I would obtain the desired array, but as I have it as a string, no numeric array gets declared.

I'm probably not explaining myself correctly, but I think with my actual output and desired one it is clearer.

data <- structure(list(first = c(22, 26, 29, 32, 36, 39, 43, 47), last = c(24, 
27, 30, 34, 37, 41, 45, 49), range = c("22:24", "26:27", "29:30", 
"32:34", "36:37", "39:41", "43:45", "47:49")), row.names = c(NA, 
-8L), class = "data.frame")

Actual output:

> paste0('c(',paste(datos2$range,collapse=','),')')
#  [1] "c(22:24,26:27,29:30,32:34,36:37,39:41,43:45,47:49)"

Desired output:

> c(22:24,26:27,29:30,32:34,36:37,39:41,43:45,47:49)
# [1] 22 23 24 26 27 29 30 32 33 34 36 37 39 40 41 43 44 45 47 48 49

I'm sure I'm doing things harder than they have to be but I can't come up with any other approach to my problem.

Can someone help me?

CodePudding user response:

mapply(seq, data$first, data$last) |> unlist()
# [1] 22 23 24 26 27 29 30 32 33 34 36 37 39 40 41 43 44 45 47 48 49

CodePudding user response:

You could use the following code using eval and parse with your code like this:

eval(parse(text = paste0('c(',paste(data$range,collapse=','),')')))
#>  [1] 22 23 24 26 27 29 30 32 33 34 36 37 39 40 41 43 44 45 47 48 49

Created on 2022-11-11 with reprex v2.0.2

CodePudding user response:

Does this tidyverse solution work for you?

library(tidyverse)
data %>%
  mutate(range = str_extract_all(range, "\\d "),
         range = lapply(range, function(x) seq(x[1], x[2])))
  first last      range
1    22   24 22, 23, 24
2    26   27     26, 27
3    29   30     29, 30
4    32   34 32, 33, 34
5    36   37     36, 37
6    39   41 39, 40, 41
7    43   45 43, 44, 45
8    47   49 47, 48, 49

If you need the sequence as a vector (rather than a list), add unnest_longer to the pipe:

data %>%
  mutate(range = str_extract_all(range, "\\d "),
         range = lapply(range, function(x) seq(x[1], x[2]))) %>%
  unnest_longer(range)
# A tibble: 21 × 3
   first  last range
   <dbl> <dbl> <int>
 1    22    24    22
 2    22    24    23
 3    22    24    24
 4    26    27    26
 5    26    27    27
 6    29    30    29
 7    29    30    30
 8    32    34    32
 9    32    34    33
10    32    34    34
# … with 11 more rows
# ℹ Use `print(n = ...)` to see more rows
  • Related