Using a vector's print method in a data frame

后端 未结 1 1953
慢半拍i
慢半拍i 2021-01-13 03:16

Consider the following vector x

x <- c(6e+06, 75000400, 743450000, 340000, 4300000)

I wish to print x in milli

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 04:10

    you need a format method and an as.data.frame method, like these:

    x <- c(6e+06, 75000400, 743450000, 340000, 4300000)
    class(x) <- 'million'
    format.million <- function(x,...)paste0(round(unclass(x) / 1e6, 1), "M")
    as.data.frame.million <- base:::as.data.frame.factor
    data.frame(x)
    

    0 讨论(0)
提交回复
热议问题