How to compress imported column types in R -
my code importing specific file far looks like
df <- read_excel("file path", col_types = c("numeric", "text", "numeric", "numeric", "numeric", "numeric", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text"), skip = 8) how go compressing "col types" sections still keeping same effect. i've tried sapply(df, as.numeric), changes columns numeric need second text.
note: understand other columns besides second have "text", example halfway point of attempt.
note read_excel guess types if not work on spreadsheet:
1) rep use rep this:
col_types <- rep(c("numeric", "text", "numeric", "text", "numeric", "text"), c(1l, 1l, 4l, 67l, 6l, 13l)) # test - col_types_orig defined in note @ end identical(col_types, col_types_orig) ## [1] true 2) rle can compress using rle , uncompress using inverse.rle:
r <- rle(col_types_orig) col_types <- inverse.rle(r) identical(inverse.rle(r), col_types_orig) ## [1] true you can r r code using dput(r). (in fact got arguments rep in (1) examining dput output.)
3) noting there 92 elements in col_types_orig , text except few numeric can this:
length(col_types_orig) ## [1] 92 table(col_types_orig) ## col_types_orig ## numeric text ## 11 81 which(col_types_orig == "numeric") ## [1] 1 3 4 5 6 74 75 76 77 78 79 col_types <- replace(rep("text", 92), c(1, 3:6, 74:79), "numeric") identical(col_types, col_types_orig) ## [1] true note:
col_types_orig <- c("numeric", "text", "numeric", "numeric", "numeric", "numeric", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text"
Comments
Post a Comment