6

我希望my.stringregion = 2. 所有其他点应保持原样。

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

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

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

以下是my.string外观:

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

4 回答 4

8

你有没有尝试过:

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

对于有条件的替换尝试:

> 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
于 2013-01-28T23:56:32.360 回答
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"
于 2013-01-28T23:57:54.980 回答
3

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 
    )
于 2013-01-29T00:01:59.093 回答
3

一个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
于 2013-01-29T00:09:54.407 回答