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

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

How replace just leading number for a string where those characters appear also as unit

问题

我有以下的向量:

  1. vector <- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63,
  2. 34, 44, 54, 64, 35, 45, 55, 65, 36, 46, 56, 66)

我想要只替换以数字开头的部分,而不转换该数字当它出现在末尾时。

  1. str_replace_all(vector, c('3' = 'Air_', '4' = 'Car_',
  2. '5' = 'Polluted_', '6' = 'Gas_'))

期望的输出是使用每个最后/末位数字作为标记,例如:

无论何时有一个3(末位数字),代码应该将其转换为:

  1. 'Air_(末位数字)'

更明确地说:

  1. # [1] "Air_1" "Car_1" "Polluted_1"
  2. # [4] "Gas_1" "Air_2" "Car_2"
  3. # [7] "Polluted_2" "Gas_2" "Air_3"
  4. # [10] "Car_3" "Polluted_3" "Gas_3"
  5. # [13] "Air_4" "Car_4" "Polluted_4"
  6. # [16] "Gas_4" "Air_5" "Car_5"
  7. # [19] "Polluted_5" "Gas_5" "Air_6"
  8. # [22] "Car_6" "Polluted_6" "Gas_6"

你是否有任何快速的方法来做到这一点?谢谢。

英文:

I have the following vector

  1. vector &lt;- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63,
  2. 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

  1. str_replace_all(vector, c(&#39;3&#39; = &#39;Air&#39;, &#39;4&#39; = &#39;Car&#39;,
  2. &#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:

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

more clearly as:

  1. # [1] &quot;Air_1&quot; &quot;Car_1&quot; &quot;Polluted_1&quot;
  2. # [4] &quot;Gas_1&quot; &quot;Air_2&quot; &quot;Car_2&quot;
  3. # [7] &quot;Polluted_2&quot; &quot;Gas_2&quot; &quot;Air_3&quot;
  4. # [10] &quot;Car_3&quot; &quot;Polluted_3&quot; &quot;Gas_3&quot;
  5. # [13] &quot;Air_4&quot; &quot;Car_4&quot; &quot;Polluted_4&quot;
  6. # [16] &quot;Gas_4&quot; &quot;Air_5&quot; &quot;Car_5&quot;
  7. # [19] &quot;Polluted_5&quot; &quot;Gas_5&quot; &quot;Air_6&quot;
  8. # [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:

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

答案2

得分: 2

这是另一种解决方案:

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

Here's another solution:

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

答案3

得分: 1

  1. library("stringr")
  2. vector <- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63, 34, 44, 54,
  3. 64, 35, 45, 55, 65, 36, 46, 56, 66)
  4. paste0(
  5. str_replace_all(stringr::str_extract(vector, "[[:digit:]]"),
  6. c('3' ='Air', '4'='Car',
  7. '5' = 'Polluted', '6' = 'Gas')),
  8. "_(",
  9. str_extract(vector, "[[:digit:]]$"),
  10. ")"
  11. )
  12. #> [1] "Air_(1)" "Car_(1)" "Polluted_(1)" "Gas_(1)" "Air_(2)"
  13. #> [6] "Car_(2)" "Polluted_(2)" "Gas_(2)" "Air_(3)" "Car_(3)"
  14. #> [11] "Polluted_(3)" "Gas_(3)" "Air_(4)" "Car_(4)" "Polluted_(4)"
  15. #> [16] "Gas_(4)" "Air_(5)" "Car_(5)" "Polluted_(5)" "Gas_(5)"
  16. #> [21] "Air_(6)" "Car_(6)" "Polluted_(6)" "Gas_(6)"

Created on 2023-07-17 with reprex v2.0.2

  1. <details>
  2. <summary>英文:</summary>
  3. You may be looking for fancier solution, but is this what you&#39;re looking for?
  4. ``` r
  5. library(&quot;stringr&quot;)
  6. vector &lt;- c(31, 41, 51, 61, 32, 42, 52, 62, 33, 43, 53, 63, 34, 44, 54,
  7. 64, 35, 45, 55, 65, 36, 46, 56, 66)
  8. paste0(
  9. str_replace_all(stringr::str_extract(vector, &quot;[[:digit:]]&quot;),
  10. c(&#39;3&#39; =&#39;Air&#39;, &#39;4&#39;=&#39;Car&#39;,
  11. &#39;5&#39; = &#39;Polluted&#39;, &#39;6&#39; = &#39;Gas&#39;)),
  12. &quot;_(&quot;,
  13. str_extract(vector, &quot;[[:digit:]]$&quot;),
  14. &quot;)&quot;
  15. )
  16. #&gt; [1] &quot;Air_(1)&quot; &quot;Car_(1)&quot; &quot;Polluted_(1)&quot; &quot;Gas_(1)&quot; &quot;Air_(2)&quot;
  17. #&gt; [6] &quot;Car_(2)&quot; &quot;Polluted_(2)&quot; &quot;Gas_(2)&quot; &quot;Air_(3)&quot; &quot;Car_(3)&quot;
  18. #&gt; [11] &quot;Polluted_(3)&quot; &quot;Gas_(3)&quot; &quot;Air_(4)&quot; &quot;Car_(4)&quot; &quot;Polluted_(4)&quot;
  19. #&gt; [16] &quot;Gas_(4)&quot; &quot;Air_(5)&quot; &quot;Car_(5)&quot; &quot;Polluted_(5)&quot; &quot;Gas_(5)&quot;
  20. #&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()来替换第一个数字的匹配。

  1. library(stringr)
  2. str_replace(vector, "^.",
  3. ~ c('3'= 'Air_', '4'= 'Car_', '5'= 'Polluted_', '6'= 'Gas_')[.x])
  4. # [1] "Air_1" "Car_1" "Polluted_1" "Gas_1" "Air_2" "Car_2"
  5. # [7] "Polluted_2" "Gas_2" "Air_3" "Car_3" "Polluted_3" "Gas_3"
  6. # [13] "Air_4" "Car_4" "Polluted_4" "Gas_4" "Air_5" "Car_5"
  7. # [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.

  1. library(stringr)
  2. str_replace(vector, &quot;^.&quot;,
  3. ~ 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])
  4. # [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;
  5. # [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;
  6. # [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;
  7. # [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,如下所示:

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

Try paste0 like this

  1. &gt; paste0(c(&quot;Air&quot;, &quot;Car&quot;, &quot;Polluted&quot;, &quot;Gas&quot;)[vector %/% 10 - 2], &quot;_&quot;, vector %% 10)
  2. [1] &quot;Air_1&quot; &quot;Car_1&quot; &quot;Polluted_1&quot; &quot;Gas_1&quot; &quot;Air_2&quot;
  3. [6] &quot;Car_2&quot; &quot;Polluted_2&quot; &quot;Gas_2&quot; &quot;Air_3&quot; &quot;Car_3&quot;
  4. [11] &quot;Polluted_3&quot; &quot;Gas_3&quot; &quot;Air_4&quot; &quot;Car_4&quot; &quot;Polluted_4&quot;
  5. [16] &quot;Gas_4&quot; &quot;Air_5&quot; &quot;Car_5&quot; &quot;Polluted_5&quot; &quot;Gas_5&quot;
  6. [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:

确定