R equivalent to Stata's `compress` command?

后端 未结 1 2000
盖世英雄少女心
盖世英雄少女心 2021-02-14 08:28

Stata has a command called compress which looks through all the data rows and tries to coerce each to the most efficient format. For instance, if you have a bunch

相关标签:
1条回答
  • 2021-02-14 08:53

    Technically, read.table does exactly that with the help of type.convert. So you could use that - it is not the most efficient way but probably the easiest:

    df <- as.data.frame(lapply(df ,function(x) type.convert(as.character(x))))
    

    In practice it may be better to do that selectively, though, so you only touch characters/factors:

    for (i in seq.int(df)) if (is.factor(df[[i]]) || is.character(df[[i]]))
        df[[i]] <- type.convert(as.character(df[[i]]))
    
    0 讨论(0)
提交回复
热议问题