强制gsub保持尾随零 [英] Force gsub to keep trailing zeros
本文介绍了强制gsub保持尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想提取."之后的数字.从data.frame [,1]中存储,并将它们存储在第二列中,如下所示:
I want to extract the digits after "." from data.frame[ ,1] and store them in a second column as following:
1 6.354 354
2 6.355 355
3 6.363 363
4 6.367 367
5 6.378 378
6 6.419 419
7 6.426 426
8 6.427 427
9 6.428 428
10 6.431 431
11 6.460 46
12 6.477 477
(...)
为此,我使用gsub(".*\\.", "", data.frame[,1])
,但这会忽略零,如您所见.在第11行中
如何提取."之后的完整数字/所有数字?
该列为数字.
To do that I use gsub(".*\\.", "", data.frame[,1])
but this ignores the zeros as you can see e.g. in row 11.
How can I extract the complete number/all digits after the "."?
The column is numeric.
推荐答案
如果为数字,则可能需要使用format
或sprintf
将其转换为character
,然后使用sub
>
If this is numeric, then we may need to convert it to character
using format
or sprintf
, and then use sub
sub("^[^.]*[.]", "", format(v1, width = max(nchar(v1))))
#[1] "354" "355" "460" "400" "012"
数据
v1 <- c(6.354, 6.355, 6.460, 6.400, 6.012)
这篇关于强制gsub保持尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文