List of data in R -
i manage data in 3 different classes - tbl_df, tbl, , data.frame - reasons.
what try make list of several data belows:
data1 tbl_df, tbl, , data.frame. data2 tbl_df, tbl, , data.frame. data3 tbl_df, tbl, , data.frame. list[1] means data1 list[2] means data2 list[3] means data3
however, surprised realised hard find how this. i'd appreciate have answer this. thanks.
when coerce data.frame
tbl_df
dplyr
package, object receives 2 more class designations, namely tbl_df
, tbl
.
library(dplyr) class(tbl_df(data.frame(x = runif(5), y = runif(5)))) [1] "tbl_df" "tbl" "data.frame"
they more or less same data.frames
, little difference when comes printing. ?tbl_df
:
the main advantage using tbl_df on regular data frame printing: tbl objects print few rows , columns fit on 1 screen, describing rest of text.
bottom line can use object of class tbl_df
, tbl
, data.frame
data.frame`.
if mean calculate mean of whole data set, perhaps better structure matrix
?
in case, in below example calculate means of columns , store result in list. example can optimized using sapply
, lapply
functions.
library(dplyr) data1 <- tbl_df(data.frame(x = runif(5), y = runif(5))) data2 <- tbl_df(data.frame(x = runif(5), y = runif(5))) data3 <- tbl_df(data.frame(x = runif(5), y = runif(5))) list.of.dfs <- ls(pattern = "data") list.of.means <- vector("list", 3) names(list.of.means) <- list.of.dfs (i in list.of.dfs) { my.df <- get(i) list.of.means[[i]] <- colmeans(my.df) } > list.of.means $data1 x y 0.4895666 0.4219187 $data2 x y 0.4100487 0.2763898 $data3 x y 0.6123135 0.5033225
Comments
Post a Comment