r - Should I use a data.frame or a matrix? -
when should 1 use data.frame
, , when better use matrix
?
both keep data in rectangular format, it's unclear.
are there general rules of thumb when use data type?
part of answer contained in question: use data frames if columns (variables) can expected of different types (numeric/character/logical etc.). matrices data of same type.
consequently, choice matrix/data.frame problematic if have data of same type.
the answer depends on going data in data.frame/matrix. if going passed other functions expected type of arguments of these functions determine choice.
also:
matrices more memory efficient:
m = matrix(1:4, 2, 2) d = as.data.frame(m) object.size(m) # 216 bytes object.size(d) # 792 bytes
matrices necessity if plan linear algebra-type of operations.
data frames more convenient if refer columns name (via compact $ operator).
data frames imho better reporting (printing) tabular information can apply formatting each column separately.
Comments
Post a Comment