如何仅替换字符串中仅出现在单位字符中的前导数字

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

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 &lt;- 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(&#39;3&#39; = &#39;Air&#39;, &#39;4&#39; = &#39;Car&#39;,
                          &#39;5&#39; = &#39;Polluted&#39;, &#39;6&#39; = &#39;Gas&#39;))

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:

&#39;Air_(unit/digit)&#39;

more clearly as:

#  [1] &quot;Air_1&quot;      &quot;Car_1&quot;      &quot;Polluted_1&quot;
#  [4] &quot;Gas_1&quot;      &quot;Air_2&quot;      &quot;Car_2&quot;     
#  [7] &quot;Polluted_2&quot; &quot;Gas_2&quot;      &quot;Air_3&quot;     
# [10] &quot;Car_3&quot;      &quot;Polluted_3&quot; &quot;Gas_3&quot;     
# [13] &quot;Air_4&quot;      &quot;Car_4&quot;      &quot;Polluted_4&quot;
# [16] &quot;Gas_4&quot;      &quot;Air_5&quot;      &quot;Car_5&quot;     
# [19] &quot;Polluted_5&quot; &quot;Gas_5&quot;      &quot;Air_6&quot;     
# [22] &quot;Car_6&quot;      &quot;Polluted_6&quot; &quot;Gas_6&quot; 

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(&#39;3&#39; = &#39;Air&#39;, &#39;4&#39; = &#39;Car&#39;, &#39;5&#39; = &#39;Polluted&#39;, &#39;6&#39; = &#39;Gas&#39;)[
    as.character(vector %/% 10) ],
  vector %% 10, sep = &quot;_&quot;)
#  [1] &quot;Air_1&quot;      &quot;Car_1&quot;      &quot;Polluted_1&quot;
#  [4] &quot;Gas_1&quot;      &quot;Air_2&quot;      &quot;Car_2&quot;     
#  [7] &quot;Polluted_2&quot; &quot;Gas_2&quot;      &quot;Air_3&quot;     
# [10] &quot;Car_3&quot;      &quot;Polluted_3&quot; &quot;Gas_3&quot;     
# [13] &quot;Air_4&quot;      &quot;Car_4&quot;      &quot;Polluted_4&quot;
# [16] &quot;Gas_4&quot;      &quot;Air_5&quot;      &quot;Car_5&quot;     
# [19] &quot;Polluted_5&quot; &quot;Gas_5&quot;      &quot;Air_6&quot;     
# [22] &quot;Car_6&quot;      &quot;Polluted_6&quot; &quot;Gas_6&quot; 

答案2

得分: 2

这是另一种解决方案:

str_replace_all(vector, c('^3' = 'Air_', '^4' = 'Car_',
                       '^5' = 'Polluted_', '^6' = 'Gas_'))
英文:

Here's another solution:

str_replace_all(vector, c(&#39;^3&#39; =&#39;Air_&#39;, &#39;^4&#39;=&#39;Car_&#39;,
                           &#39;^5&#39; = &#39;Polluted_&#39;, &#39;^6&#39; = &#39;Gas_&#39;))

答案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&#39;re looking for?

``` r
library(&quot;stringr&quot;)
vector &lt;- 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, &quot;[[:digit:]]&quot;),
                  c(&#39;3&#39; =&#39;Air&#39;, &#39;4&#39;=&#39;Car&#39;,
                    &#39;5&#39; = &#39;Polluted&#39;, &#39;6&#39; = &#39;Gas&#39;)),
  &quot;_(&quot;,
  str_extract(vector, &quot;[[:digit:]]$&quot;),
  &quot;)&quot;
)
#&gt;  [1] &quot;Air_(1)&quot;      &quot;Car_(1)&quot;      &quot;Polluted_(1)&quot; &quot;Gas_(1)&quot;      &quot;Air_(2)&quot;     
#&gt;  [6] &quot;Car_(2)&quot;      &quot;Polluted_(2)&quot; &quot;Gas_(2)&quot;      &quot;Air_(3)&quot;      &quot;Car_(3)&quot;     
#&gt; [11] &quot;Polluted_(3)&quot; &quot;Gas_(3)&quot;      &quot;Air_(4)&quot;      &quot;Car_(4)&quot;      &quot;Polluted_(4)&quot;
#&gt; [16] &quot;Gas_(4)&quot;      &quot;Air_(5)&quot;      &quot;Car_(5)&quot;      &quot;Polluted_(5)&quot; &quot;Gas_(5)&quot;     
#&gt; [21] &quot;Air_(6)&quot;      &quot;Car_(6)&quot;      &quot;Polluted_(6)&quot; &quot;Gas_(6)&quot;

<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, &quot;^.&quot;,
  ~ c(&#39;3&#39;= &#39;Air_&#39;, &#39;4&#39;= &#39;Car_&#39;, &#39;5&#39;= &#39;Polluted_&#39;, &#39;6&#39;= &#39;Gas_&#39;)[.x])

#  [1] &quot;Air_1&quot;      &quot;Car_1&quot;      &quot;Polluted_1&quot; &quot;Gas_1&quot;      &quot;Air_2&quot;      &quot;Car_2&quot;     
#  [7] &quot;Polluted_2&quot; &quot;Gas_2&quot;      &quot;Air_3&quot;      &quot;Car_3&quot;      &quot;Polluted_3&quot; &quot;Gas_3&quot;     
# [13] &quot;Air_4&quot;      &quot;Car_4&quot;      &quot;Polluted_4&quot; &quot;Gas_4&quot;      &quot;Air_5&quot;      &quot;Car_5&quot;     
# [19] &quot;Polluted_5&quot; &quot;Gas_5&quot;      &quot;Air_6&quot;      &quot;Car_6&quot;      &quot;Polluted_6&quot; &quot;Gas_6&quot;

答案5

得分: 0

尝试使用paste0,如下所示:

&gt; 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

&gt; paste0(c(&quot;Air&quot;, &quot;Car&quot;, &quot;Polluted&quot;, &quot;Gas&quot;)[vector %/% 10 - 2], &quot;_&quot;, vector %% 10)
 [1] &quot;Air_1&quot;      &quot;Car_1&quot;      &quot;Polluted_1&quot; &quot;Gas_1&quot;      &quot;Air_2&quot;
 [6] &quot;Car_2&quot;      &quot;Polluted_2&quot; &quot;Gas_2&quot;      &quot;Air_3&quot;      &quot;Car_3&quot;
[11] &quot;Polluted_3&quot; &quot;Gas_3&quot;      &quot;Air_4&quot;      &quot;Car_4&quot;      &quot;Polluted_4&quot;
[16] &quot;Gas_4&quot;      &quot;Air_5&quot;      &quot;Car_5&quot;      &quot;Polluted_5&quot; &quot;Gas_5&quot;
[21] &quot;Air_6&quot;      &quot;Car_6&quot;      &quot;Polluted_6&quot; &quot;Gas_6&quot;

huangapple
  • 本文由 发表于 2023年7月17日 23:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705798.html
匿名

发表评论

匿名网友

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

确定