Question

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
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top