如何解析一系列数字?

huangapple go评论64阅读模式
英文:

How to parse a range of numbers?

问题

Sure, here's the translated code part:

我有一些数据看起来像这样:

> dput(range)
structure(list(Range = c("0T-1T", "2T-5T")), class = "data.frame", row.names = c(NA, 
-2L))

基本上,我想知道是否可以解析这些值,以便最终的数据框看起来像这样:

> dput(range2)
structure(list(Range = c("0T", "1T", "2T", "3T", "4T", "5T")), class = "data.frame", row.names = c(NA, 
-6L))
英文:

I have some data that looks like this:

> dput(range)
structure(list(Range = c("0T-1T", "2T-5T")), class = "data.frame", row.names = c(NA, 
-2L))

Basically, I am wondering if it is possible to parse these values out, so that the final dataframe looks like this:

> dput(range2)
structure(list(Range = c("0T", "1T", "2T", "3T", "4T", "5T")), class = "data.frame", row.names = c(NA, 
-6L))

答案1

得分: 1

A tidyverse solution with tidyr::separate_longer_delim() + tidyr::full_seq():

library(tidyverse)

range %>%
  group_by(grp = Range) %>%
  separate_longer_delim(Range, '-') %>%
  reframe(Range = paste0(full_seq(parse_number(Range), 1), 'T')) %>%
  select(-grp)

# A tibble: 6 × 1

Range

1 0T

2 1T

3 2T

4 3T

5 4T

6 5T


<details>
<summary>英文:</summary>

A `tidyverse` solution with `tidyr::separate_longer_delim()` + `tidyr::full_seq()`:

```r
library(tidyverse)

range %&gt;%
  group_by(grp = Range) %&gt;%
  separate_longer_delim(Range, &#39;-&#39;) %&gt;%
  reframe(Range = paste0(full_seq(parse_number(Range), 1), &#39;T&#39;)) %&gt;%
  select(-grp)

# # A tibble: 6 &#215; 1
#   Range
#   &lt;chr&gt;
# 1 0T
# 2 1T   
# 3 2T
# 4 3T
# 5 4T
# 6 5T

答案2

得分: 0

可能有更简洁的方法,但这是一种方法:

library(dplyr, warn.conflicts = F)
library(stringr)
library(tidyr)
library(purrr)
structure(list(Range = c("0T-1T", "2T-5T")), class = "data.frame", row.names = c(NA, -2L)) %>%
    mutate(
        Range = str_split(Range, '-'),
        Range = map(Range, str_remove, 'T'),
        Range = map(Range, as.integer),
        Range = map(Range, ~ paste0('T', seq(.x[1], .x[2], 1)))
    ) %>%
    unnest(Range)
#> # A tibble: 6 × 1
#>   Range
#>   <chr>
#> 1 T0   
#> 2 T1   
#> 3 T2   
#> 4 T3   
#> 5 T4   
#> 6 T5

创建于2023年06月08日,使用reprex v2.0.2

假设仅在每个"XT-YT"对内展开,而不是整个范围(这肯定更容易)。

英文:

There is probably a cleaner way but here is one way:

library(dplyr, warn.conflicts = F)
library(stringr)
library(tidyr)
library(purrr)
structure(list(Range = c(&quot;0T-1T&quot;, &quot;2T-5T&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
                                                                                 -2L)) %&gt;% 
    mutate(
        Range = str_split(Range, &#39;-&#39;),
        Range = map(Range, str_remove, &#39;T&#39;),
        Range = map(Range, as.integer),
        Range = map(Range, ~ paste0(&#39;T&#39;, seq(.x[1], .x[2], 1)))
    ) %&gt;% 
    unnest(Range)
#&gt; # A tibble: 6 &#215; 1
#&gt;   Range
#&gt;   &lt;chr&gt;
#&gt; 1 T0   
#&gt; 2 T1   
#&gt; 3 T2   
#&gt; 4 T3   
#&gt; 5 T4   
#&gt; 6 T5

<sup>Created on 2023-06-08 with reprex v2.0.2</sup>

Assumes to only expand withing each "XT-YT" pair rather than the full range (which would be easier for sure).

huangapple
  • 本文由 发表于 2023年6月9日 05:15:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定