我有以下数据,
id <- c("case1", "case19", "case88", "case77")
vec <- c("One_20 (19)",
"tWo_20 (290)",
"Three_38 (399)",
NA)
df <- data.frame(id, vec)
> df
id vec
1 case1 One_20 (19)
2 case19 tWo_20 (290)
3 case88 Three_38 (399)
4 case77 <NA>
我想将vec
向量分成两个变量,即:txt
和。我更喜欢这样num
使用,tidyr
df |> tidyr::separate_wider_regex(vec,
c(txt = "[A-Za-z]+", num = "\\d+"),
too_few = "align_start")
# A tibble: 4 × 3
id txt num
<chr> <chr> <chr>
1 case1 One NA
2 case19 tWo NA
3 case88 Three NA
4 case77 NA NA
但是,这不是我想要的。我有以下期望:
id txt num
1 case1 One_20 19
2 case19 tWo_20 290
3 case88 Three_38 399
4 case77 <NA> NA
我在正则表达式部分犯了错误。有什么帮助可以纠正这些错误,以便我能够得到预期的表格作为输出吗?