Reputation: 1992
I have a vector like this:
a <- c(11223344,55667788)
I would like to create a new vector cutting of the last two numbers of each entry in a:
[1] 112233 556677
Do I have to use regex to achieve this or is there a simple indexing trick that I'm not aware of?
Upvotes: 2
Views: 86
Reputation: 99331
You can use substr
(or substring
), cutting the number of characters off at two before the end
a <- c(11223344, 55667788)
substr(a, 1, nchar(a)-2)
# [1] "112233" "556677"
wrapping in as.numeric
if necessary.
Upvotes: 3
Reputation: 145775
If they're integers:
> trunc(a / 100)
[1] 112233 556677
Only if they're strictly positive, you could use floor:
> floor(a / 100)
[1] 112233 556677
Upvotes: 5
Reputation: 887128
Or you could use sub
as.numeric(sub('..$','', a))
#[1] 112233 556677
Upvotes: 6