sapply vs. lapply while reading files and rbind'ing them

痞子三分冷 提交于 2019-12-08 19:44:27

The issue had nothing to do with factors, it's generic sapply vs lapply. Why does sapply get it so wrong whereas lapply gets it right? Remember in R, dataframes are lists-of-columns. and each column can have a distinct type.

  • lapply returns a list-of-columns to rbind, which does the concatenation correctly. It keeps corresponding columns together. So your factors emerge correctly.
  • sapply however...
    • returns a matrix of numeric... (since matrices can only have one type, unlike dataframes)
    • ...which, worse still, has an unwanted transpose
    • so sapply turns your two 5x6 input dataframes into transposed 6x5 matrices (columns now correspond to rows)...
    • with all data coerced to numeric (garbage!).
    • then rbind row-"concatenates" those two garbage 6x5 matrices of numeric into one very-garbage 12x5 matrix. Since columns have been transposed into rows, row-concatenating the matrices combines datatypes, and obviously your factors are messed up.

Summary: just use lapply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!