英文:
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 %>%
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
# <chr>
# 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("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
<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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论