我有以下数字。我需要写一个正则表达式来排除集合中的第一个数字(24.00)。我怎么能这样做,或者更具体地说在R?我试过[^ 24.00]但没有运气。请帮忙。
> 24.00 32824.637 15228.005 12171.46 4468.862 12898.464 5344.394 2342.57
5029.098 725193.312 703328.437 667.072 2707.375
15991.489 11449.667 5172.415 2345.175 3788.761 3543.821 1891.775 4917.051
714035.5 691709.438 682.707 1873.534
9659.517 11066.622 3327.543 1761.487 1868.323 3421.35 1175.193 3507.34
528521.687 516397.187 250.152 1417.528
5654.478 10415.184 1519.152 1451.403 898.984 3038.305 682.707 2918.44
184953.547 173514.313 343.959 865.109
2465.04 10386.521 664.466 1443.586 307.479 3517.763 523.756 2749.067
42770.785 43617.656 349.171 1024.06
854.686 10852.95 205.854 1732.824 80.778 3317.12 453.401 2876.748 10751.326
10954.574 221.489 1011.031
244.941 10058.196 75.567 1537.393 52.115 3176.41 479.458 2822.028 3134.718
3343.178 333.536 971.945
72.961 10089.465 41.692 1506.124 46.904 3017.459 495.093 2707.375 304.873
760.879 237.123 979.762
答案 0 :(得分:2)
我们可以使用sub
将一个或多个数字的模式与字符串的开头(^
)中的点匹配,然后将其替换为空白(""
)
sub("^[0-9.]+\\s*", "", trimws(txt))
#[1] "32824.637 15228.005 12171.46 4468.862 12898.464 5344.394 2342.57 5029.098 725193.312 703328.437 667.072 2707.375"
如果这些行是单独的字符串,则上述代码应该适用于'n'行数
sub("^[0-9.]+\\s*", "", trimws(txt1))
#[1] "32824.637 15228.005 12171.46 4468.862 12898.464 5344.394 2342.57 5029.098 725193.312 703328.437 667.072 2707.375"
#[2] "32824.637 15228.005 12171.46 4468.862 12898.464 5344.394 2342.57 5029.098 725193.312 703328.437 667.072 2707.375"
txt <- c(" 24.00 32824.637 15228.005 12171.46 4468.862 12898.464 5344.394 2342.57 5029.098 725193.312 703328.437 667.072 2707.375")
txt1 <- c(txt, txt)
答案 1 :(得分:1)
以下是使用sub
:
x <- c("24.00 32824.637 15228.005 12171.46 4468.862 12898.464 5344.394 2342.57 5029.098 725193.312 703328.437 667.072 2707.375")
y <- sub("\\S+ ", "", x) # substitute everything up to the first space with the empty string
y
# [1] "32824.637 15228.005 12171.46 4468.862 12898.464 5344.394 2342.57 5029.098 725193.312 703328.437 667.072 2707.375"