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

社会主义新天地 提交于 2019-12-07 07:57:14

问题


I have a data frame with some very long "comments" columns. When I have them displayed they are broken into different blocks, making it hard to read across rows. Is it possible to change a setting in R or modify the call to data.frame to truncate strings at a certain length?

Example: a 3-column dataframe

data.frame(cbind(rep(1,5),rep(c("very very long obnoxious character string here" ,"dog","cat","dog",5)),rep(c("very very long obnoxious character string here" ,"dog","cat","dog",5))))

Resulting dataframe as seen on my screen:

  X1                                             X2
1  1 very very long obnoxious character string here
2  1                                            dog
3  1                                            cat
4  1                                            dog
5  1                                              5
                                          X3
1 very very long obnoxious character string here
2                                            dog
3                                            cat
4                                            dog
5                                              5

回答1:


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


来源:https://stackoverflow.com/questions/8379988/is-it-possible-to-truncate-output-when-viewing-the-contents-of-dataframes

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