替换字符串的前N个点

时间:2021-09-22 09:31:31

I hope to replace the first 14 dots of my.string with 14 zeroes when region = 2. All other dots should be kept the way they are.

我希望当region = 2时,my.string的前14个点用14个零替换。所有其他点应保持原样。

df.1 = read.table(text = "
  city  county  state region                        my.string reg1 reg2
   1      1        1      1    123456789012345678901234567890   1    0
   1      2        1      1    ...................34567890098   1    0
   1      1        2      1    112233..............0099887766   1    0
   1      2        2      1    ..............2020202020202020   1    0
   1      1        1      2    ..............00..............   0    1
   1      2        1      2    ..............0987654321123456   0    1
   1      1        2      2    ..............9999988888777776   0    1
   1      2        2      2    ..................555555555555   0    1
", sep = "", header = TRUE, stringsAsFactors = FALSE)

df.1

I do not think this question has been asked here. Sorry if it has. Sorry also not to have spent more time looking for the solution. A quick Google search did not turn up an answer. I did ask a similar question here earlier: R: removing the last three dots from a string Thank you for any help.

我不认为这里有这个问题。对不起,如果有的话。对不起,也没有花更多时间寻找解决方案。快速谷歌搜索没有找到答案。我之前在这里问过类似的问题:R:从字符串中删除最后三个点谢谢你的帮助。

I should clarify that I only want to remove 14 consecutive dots at the far left of the string. If a string begins with a number that is followed by 14 dots, then those 14 dots should remain the way they are.

我应该澄清一下,我只想删除字符串最左边的14个连续点。如果一个字符串以一个后跟14个点的数字开头,那么这14个点应该保持原样。

Here is how my.string would look:

以下是my.string的外观:

123456789012345678901234567890
...................34567890098
112233..............0099887766
..............2020202020202020
0000000000000000..............
000000000000000987654321123456
000000000000009999988888777776
00000000000000....555555555555

4 个解决方案

#1


8  

Have you tried:

你有没有尝试过:

sub("^\\.{14}", "00000000000000", df.1$my.string )

For conditional replacement try:

对于条件替换尝试:

> df.1[ df.1$region ==2, "mystring"] <- 
               sub("^\\.{14}", "00000000000000", df.1$my.string[ df.1$region==2] )
> df.1
  city county state region                      my.string reg1 reg2
1    1      1     1      1 123456789012345678901234567890    1    0
2    1      2     1      1 ...................34567890098    1    0
3    1      1     2      1 112233..............0099887766    1    0
4    1      2     2      1 ..............2020202020202020    1    0
5    1      1     1      2 ..............00..............    0    1
6    1      2     1      2 ..............0987654321123456    0    1
7    1      1     2      2 ..............9999988888777776    0    1
8    1      2     2      2 ..................555555555555    0    1
                        mystring
1                           <NA>
2                           <NA>
3                           <NA>
4                           <NA>
5 0000000000000000..............
6 000000000000000987654321123456
7 000000000000009999988888777776
8 00000000000000....555555555555

#2


3  

    gsub('^[.]{14,14}',paste(rep(0,14),collapse=''),df.1$my.string)
"123456789012345678901234567890" "00000000000000.....34567890098" "112233..............0099887766"
[4] "000000000000002020202020202020" "0000000000000000.............." "000000000000000987654321123456"
[7] "000000000000009999988888777776" "00000000000000....555555555555"

#3


3  

dwin's answer is awesome. here's one that's easy to understand but not nearly as spiffy

dwin的答案很棒。这是一个容易理解但不是那么漂亮的东西

# restrict the substitution to only region == 2..
# then replace the 'my.string' column with..
df.1[ df.1$region == 2 , 'my.string' ] <- 

    # substitute.. (only the first instance!)
    # (use gsub for multiple instances)
    sub( 
        # fourteen dots
        '..............' , 
        # with fourteen zeroes
        '00000000000000' , 
        # in the same object (also restricted to region == 2
        df.1[ df.1$region == 2 , 'my.string' ] , 
        # and don't use regex or anything special.
        # just exactly 14 dots.
        fixed = TRUE 
    )

#4


3  

A data.table solution:

一个data.table解决方案:

require(data.table)
dt <- data.table(df.1)

# solution:
dt[, mystring := ifelse(region == 2, sub("^[.]{14}", 
                   paste(rep(0,14), collapse=""), my.string), 
                   my.string), by=1:nrow(dt)]

#    city county state region                      my.string reg1 reg2                       mystring
# 1:    1      1     1      1 123456789012345678901234567890    1    0 123456789012345678901234567890
# 2:    1      2     1      1 ...................34567890098    1    0 ...................34567890098
# 3:    1      1     2      1 112233..............0099887766    1    0 112233..............0099887766
# 4:    1      2     2      1 ..............2020202020202020    1    0 ..............2020202020202020
# 5:    1      1     1      2 ..............00..............    0    1 0000000000000000..............
# 6:    1      2     1      2 ..............0987654321123456    0    1 000000000000000987654321123456
# 7:    1      1     2      2 ..............9999988888777776    0    1 000000000000009999988888777776
# 8:    1      2     2      2 ..................555555555555    0    1 00000000000000....555555555555

#1


8  

Have you tried:

你有没有尝试过:

sub("^\\.{14}", "00000000000000", df.1$my.string )

For conditional replacement try:

对于条件替换尝试:

> df.1[ df.1$region ==2, "mystring"] <- 
               sub("^\\.{14}", "00000000000000", df.1$my.string[ df.1$region==2] )
> df.1
  city county state region                      my.string reg1 reg2
1    1      1     1      1 123456789012345678901234567890    1    0
2    1      2     1      1 ...................34567890098    1    0
3    1      1     2      1 112233..............0099887766    1    0
4    1      2     2      1 ..............2020202020202020    1    0
5    1      1     1      2 ..............00..............    0    1
6    1      2     1      2 ..............0987654321123456    0    1
7    1      1     2      2 ..............9999988888777776    0    1
8    1      2     2      2 ..................555555555555    0    1
                        mystring
1                           <NA>
2                           <NA>
3                           <NA>
4                           <NA>
5 0000000000000000..............
6 000000000000000987654321123456
7 000000000000009999988888777776
8 00000000000000....555555555555

#2


3  

    gsub('^[.]{14,14}',paste(rep(0,14),collapse=''),df.1$my.string)
"123456789012345678901234567890" "00000000000000.....34567890098" "112233..............0099887766"
[4] "000000000000002020202020202020" "0000000000000000.............." "000000000000000987654321123456"
[7] "000000000000009999988888777776" "00000000000000....555555555555"

#3


3  

dwin's answer is awesome. here's one that's easy to understand but not nearly as spiffy

dwin的答案很棒。这是一个容易理解但不是那么漂亮的东西

# restrict the substitution to only region == 2..
# then replace the 'my.string' column with..
df.1[ df.1$region == 2 , 'my.string' ] <- 

    # substitute.. (only the first instance!)
    # (use gsub for multiple instances)
    sub( 
        # fourteen dots
        '..............' , 
        # with fourteen zeroes
        '00000000000000' , 
        # in the same object (also restricted to region == 2
        df.1[ df.1$region == 2 , 'my.string' ] , 
        # and don't use regex or anything special.
        # just exactly 14 dots.
        fixed = TRUE 
    )

#4


3  

A data.table solution:

一个data.table解决方案:

require(data.table)
dt <- data.table(df.1)

# solution:
dt[, mystring := ifelse(region == 2, sub("^[.]{14}", 
                   paste(rep(0,14), collapse=""), my.string), 
                   my.string), by=1:nrow(dt)]

#    city county state region                      my.string reg1 reg2                       mystring
# 1:    1      1     1      1 123456789012345678901234567890    1    0 123456789012345678901234567890
# 2:    1      2     1      1 ...................34567890098    1    0 ...................34567890098
# 3:    1      1     2      1 112233..............0099887766    1    0 112233..............0099887766
# 4:    1      2     2      1 ..............2020202020202020    1    0 ..............2020202020202020
# 5:    1      1     1      2 ..............00..............    0    1 0000000000000000..............
# 6:    1      2     1      2 ..............0987654321123456    0    1 000000000000000987654321123456
# 7:    1      1     2      2 ..............9999988888777776    0    1 000000000000009999988888777776
# 8:    1      2     2      2 ..................555555555555    0    1 00000000000000....555555555555