R语言 如何替换一个字符串的前导数字,其中这些字符也作为单位出现

oxalkeyp  于 2023-07-31  发布在  其他
关注(0)|答案(5)|浏览(91)

我有下面的向量

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(unit/digit),代码应将其转换为:

'Air_(unit/digit)'


更清楚地说:

#  [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"


你能给我个快速的解决办法吗?谢啦,谢啦

34gzjxbg

34gzjxbg1#

这里有另一个解决方案:

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

字符串

gopyfrb3

gopyfrb32#

假设我们总是有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"

字符串

vwhgwdsa

vwhgwdsa3#

您可能正在寻找更好的解决方案,但这是您正在寻找的吗?

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)"

字符串
创建于2023-07-17带有reprex v2.0.2
这里假设总是有两个数字,第一个代表类别,第二个代表某种值。

e5nszbig

e5nszbig4#

使用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"

字符串

v64noz0r

v64noz0r5#

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"

字符串

相关问题