Home > Mobile >  How limit a number of digits in a integer?
How limit a number of digits in a integer?

Time:09-28

I have a specific column in a data frame where there are numbers with 7 digits to 10 digits. I need to standardize them to have just 7 digits. How can I do this in R?

I have

dado$latitude<-c (-3005575,  -3005608,  -3005351,  -3015892843, 
    -3016114564, -3016346995, ...)

I need

dado$latitude<-c (-3005575,  -3005608,  -3005351,  -3015892, -3016114,
    -3016346, ...)

CodePudding user response:

dado = data.frame(
 latitude = c (-3005575,  -3005608,  -3005351,  -3015892843, -3016114564, -3016346995),
 want = c(-3005575,  -3005608,  -3005351,  -3015892, -3016114, -3016346)
)

dado$result = as.integer(with(
  dado, ifelse(latitude < 0,
    substr(latitude, start = 1, stop = 8),
    substr(latitude, start = 1, stop = 7))
))
dado
#      latitude     want   result
# 1    -3005575 -3005575 -3005575
# 2    -3005608 -3005608 -3005608
# 3    -3005351 -3005351 -3005351
# 4 -3015892843 -3015892 -3015892
# 5 -3016114564 -3016114 -3016114
# 6 -3016346995 -3016346 -3016346

CodePudding user response:

latitude <- c(-3005575,  -3005608,  -3005351,  -3015892843, -3016114564, -3016346995)

One way to do this:

as.integer(gsub("(-?[0-9]{7}).*", "\\1", as.character(latitude))
  • -?: allow but don't require a minus sign
  • [0-9]{7}: 7 numeric values
  • .*: the rest of the string
  • \\1: substitute only the matched part (between parentheses)

(You could also use stringr::str_extract() for this.)

  • Related