Is it possible to truncate output when viewing the contents of dataframes?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 10:14:36

I recommend a kind of the explicit way like this:

f <- function(x) data.frame(lapply(x, substr, 1, 5))

usage:

> f(d)
  X1    X2    X3
1  1 very  very 
2  1   dog   dog
3  1   cat   cat
4  1   dog   dog
5  1     5     5

Although it is possible to change the default behavior, I don't recommend:

body(format.data.frame)[[5]] <- quote(for (i in 1L:nc) rval[[i]] <- substr(format(x[[i]], ..., justify = justify), 1, 5))
unlockBinding("format.data.frame", baseenv())
assign("format.data.frame", format.data.frame, pos = baseenv())
lockBinding("format.data.frame", baseenv())
rm(format.data.frame)

usage:

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