Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R equivalent to Stata's `compress` command?

Tags:

r

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 of integers stored as a character vector within a data.frame, it will coerce that to integer.

I can imagine how one might write such a function in R, but does it exist already?

like image 865
Ari B. Friedman Avatar asked Feb 20 '12 00:02

Ari B. Friedman


1 Answers

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]]))
like image 159
Simon Urbanek Avatar answered Nov 16 '22 13:11

Simon Urbanek