r - How can I efficiently construct a very long factor with few levels? -
in r, want create factor few levels, length of 100 million. "normal" way me create factor call factor on character vector, expect method inefficient. proper way construct long factor without expanding corresponding character vector.
here example of wrong way it: creating , factoring character vector:
long.char.vector = sample(c("left", "middle", "right"), replace=true, 50000000) long.factor = factor(long.char.vector) how can construct long.factor without first constructing long.char.vector? yes, know 2 lines of code can combined, resulting line of code still creates gigantic char vector anyway.
it's not going more efficient, can sample factor vector:
big.factor <- sample(factor(c("left", "middle", "right")), replace=true, 5e7)
Comments
Post a Comment