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
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