how to return number of decimal places in R

前端 未结 12 1142
走了就别回头了
走了就别回头了 2020-12-01 08:25

I am working in R. I have a series of coordinates in decimal degrees, and I would like to sort these coordinates by how many decimal places these numbers have (i.e. I will w

12条回答
  •  -上瘾入骨i
    2020-12-01 08:33

    Rollowing up on Roman's suggestion:

    num.decimals <- function(x) {
        stopifnot(class(x)=="numeric")
        x <- sub("0+$","",x)
        x <- sub("^.+[.]","",x)
        nchar(x)
    }
    x <- "5.2300000"
    num.decimals(x)
    

    If your data isn't guaranteed to be of the proper form, you should do more checking to ensure other characters aren't sneaking in.

提交回复
热议问题