英文:
How replace just leading number for a string where those characters appear also as unit
问题
我有以下的向量:
vector <- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63,
34, 44, 54, 64, 35, 45, 55, 65, 36, 46, 56, 66)
我想要只替换以数字开头的部分,而不转换该数字当它出现在末尾时。
str_replace_all(vector, c('3' = 'Air_', '4' = 'Car_',
'5' = 'Polluted_', '6' = 'Gas_'))
期望的输出是使用每个最后/末位数字作为标记,例如:
无论何时有一个3(末位数字)
,代码应该将其转换为:
'Air_(末位数字)'
更明确地说:
# [1] "Air_1" "Car_1" "Polluted_1"
# [4] "Gas_1" "Air_2" "Car_2"
# [7] "Polluted_2" "Gas_2" "Air_3"
# [10] "Car_3" "Polluted_3" "Gas_3"
# [13] "Air_4" "Car_4" "Polluted_4"
# [16] "Gas_4" "Air_5" "Car_5"
# [19] "Polluted_5" "Gas_5" "Air_6"
# [22] "Car_6" "Polluted_6" "Gas_6"
你是否有任何快速的方法来做到这一点?谢谢。
英文:
I have the following vector
vector <- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63,
34, 44, 54, 64, 35, 45, 55, 65, 36, 46, 56, 66)
I would like just to replace the leading number without convert that number when it is found as unit digit
str_replace_all(vector, c('3' = 'Air', '4' = 'Car',
'5' = 'Polluted', '6' = 'Gas'))
The desired out is just is to use each final/unit digit as a marker meaning that for example:
wherever there is a 3(unit/digit)
, the code should convert it as:
'Air_(unit/digit)'
more clearly as:
# [1] "Air_1" "Car_1" "Polluted_1"
# [4] "Gas_1" "Air_2" "Car_2"
# [7] "Polluted_2" "Gas_2" "Air_3"
# [10] "Car_3" "Polluted_3" "Gas_3"
# [13] "Air_4" "Car_4" "Polluted_4"
# [16] "Gas_4" "Air_5" "Car_5"
# [19] "Polluted_5" "Gas_5" "Air_6"
# [22] "Car_6" "Polluted_6" "Gas_6"
Would you suggest any quick way to do this? Thanks
答案1
得分: 2
paste(
c('3' = 'Air', '4' = 'Car', '5' = 'Polluted', '6' = 'Gas')[
as.character(vector %/% 10) ],
vector %% 10, sep = "_")
[1] "Air_1" "Car_1" "Polluted_1"
[4] "Gas_1" "Air_2" "Car_2"
[7] "Polluted_2" "Gas_2" "Air_3"
[10] "Car_3" "Polluted_3" "Gas_3"
[13] "Air_4" "Car_4" "Polluted_4"
[16] "Gas_4" "Air_5" "Car_5"
[19] "Polluted_5" "Gas_5" "Air_6"
[22] "Car_6" "Polluted_6" "Gas_6"
英文:
Assuming we always have 2 digits, using arithmetic operators %%
to get 1st digit and %/%
to get remainder - the 2nd digit. Then paste all as desired:
paste(
c('3' = 'Air', '4' = 'Car', '5' = 'Polluted', '6' = 'Gas')[
as.character(vector %/% 10) ],
vector %% 10, sep = "_")
# [1] "Air_1" "Car_1" "Polluted_1"
# [4] "Gas_1" "Air_2" "Car_2"
# [7] "Polluted_2" "Gas_2" "Air_3"
# [10] "Car_3" "Polluted_3" "Gas_3"
# [13] "Air_4" "Car_4" "Polluted_4"
# [16] "Gas_4" "Air_5" "Car_5"
# [19] "Polluted_5" "Gas_5" "Air_6"
# [22] "Car_6" "Polluted_6" "Gas_6"
答案2
得分: 2
这是另一种解决方案:
str_replace_all(vector, c('^3' = 'Air_', '^4' = 'Car_',
'^5' = 'Polluted_', '^6' = 'Gas_'))
英文:
Here's another solution:
str_replace_all(vector, c('^3' ='Air_', '^4'='Car_',
'^5' = 'Polluted_', '^6' = 'Gas_'))
答案3
得分: 1
library("stringr")
vector <- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63, 34, 44, 54,
64, 35, 45, 55, 65, 36, 46, 56, 66)
paste0(
str_replace_all(stringr::str_extract(vector, "[[:digit:]]"),
c('3' ='Air', '4'='Car',
'5' = 'Polluted', '6' = 'Gas')),
"_(",
str_extract(vector, "[[:digit:]]$"),
")"
)
#> [1] "Air_(1)" "Car_(1)" "Polluted_(1)" "Gas_(1)" "Air_(2)"
#> [6] "Car_(2)" "Polluted_(2)" "Gas_(2)" "Air_(3)" "Car_(3)"
#> [11] "Polluted_(3)" "Gas_(3)" "Air_(4)" "Car_(4)" "Polluted_(4)"
#> [16] "Gas_(4)" "Air_(5)" "Car_(5)" "Polluted_(5)" "Gas_(5)"
#> [21] "Air_(6)" "Car_(6)" "Polluted_(6)" "Gas_(6)"
Created on 2023-07-17 with reprex v2.0.2
<details>
<summary>英文:</summary>
You may be looking for fancier solution, but is this what you're looking for?
``` r
library("stringr")
vector <- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63, 34, 44, 54,
64, 35, 45, 55, 65, 36, 46, 56, 66)
paste0(
str_replace_all(stringr::str_extract(vector, "[[:digit:]]"),
c('3' ='Air', '4'='Car',
'5' = 'Polluted', '6' = 'Gas')),
"_(",
str_extract(vector, "[[:digit:]]$"),
")"
)
#> [1] "Air_(1)" "Car_(1)" "Polluted_(1)" "Gas_(1)" "Air_(2)"
#> [6] "Car_(2)" "Polluted_(2)" "Gas_(2)" "Air_(3)" "Car_(3)"
#> [11] "Polluted_(3)" "Gas_(3)" "Air_(4)" "Car_(4)" "Polluted_(4)"
#> [16] "Gas_(4)" "Air_(5)" "Car_(5)" "Polluted_(5)" "Gas_(5)"
#> [21] "Air_(6)" "Car_(6)" "Polluted_(6)" "Gas_(6)"
<sup>Created on 2023-07-17 with reprex v2.0.2</sup>
This assumes there are always two digits, and the first represents the category, and the second some sort of value.
答案4
得分: 1
使用stringr
,您可以将一个函数提供给str_replace()
来替换第一个数字的匹配。
library(stringr)
str_replace(vector, "^.",
~ c('3'= 'Air_', '4'= 'Car_', '5'= 'Polluted_', '6'= 'Gas_')[.x])
# [1] "Air_1" "Car_1" "Polluted_1" "Gas_1" "Air_2" "Car_2"
# [7] "Polluted_2" "Gas_2" "Air_3" "Car_3" "Polluted_3" "Gas_3"
# [13] "Air_4" "Car_4" "Polluted_4" "Gas_4" "Air_5" "Car_5"
# [19] "Polluted_5" "Gas_5" "Air_6" "Car_6" "Polluted_6" "Gas_6"
英文:
With stringr
, you can supply a function into str_replace()
to replace the match of the first number.
library(stringr)
str_replace(vector, "^.",
~ c('3'= 'Air_', '4'= 'Car_', '5'= 'Polluted_', '6'= 'Gas_')[.x])
# [1] "Air_1" "Car_1" "Polluted_1" "Gas_1" "Air_2" "Car_2"
# [7] "Polluted_2" "Gas_2" "Air_3" "Car_3" "Polluted_3" "Gas_3"
# [13] "Air_4" "Car_4" "Polluted_4" "Gas_4" "Air_5" "Car_5"
# [19] "Polluted_5" "Gas_5" "Air_6" "Car_6" "Polluted_6" "Gas_6"
答案5
得分: 0
尝试使用paste0
,如下所示:
> paste0(c("Air", "Car", "Polluted", "Gas")[vector %/% 10 - 2], "_", vector %% 10)
[1] "Air_1" "Car_1" "Polluted_1" "Gas_1" "Air_2"
[6] "Car_2" "Polluted_2" "Gas_2" "Air_3" "Car_3"
[11] "Polluted_3" "Gas_3" "Air_4" "Car_4" "Polluted_4"
[16] "Gas_4" "Air_5" "Car_5" "Polluted_5" "Gas_5"
[21] "Air_6" "Car_6" "Polluted_6" "Gas_6"
英文:
Try paste0
like this
> paste0(c("Air", "Car", "Polluted", "Gas")[vector %/% 10 - 2], "_", vector %% 10)
[1] "Air_1" "Car_1" "Polluted_1" "Gas_1" "Air_2"
[6] "Car_2" "Polluted_2" "Gas_2" "Air_3" "Car_3"
[11] "Polluted_3" "Gas_3" "Air_4" "Car_4" "Polluted_4"
[16] "Gas_4" "Air_5" "Car_5" "Polluted_5" "Gas_5"
[21] "Air_6" "Car_6" "Polluted_6" "Gas_6"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论