Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 871
N Brouwer Avatar asked Dec 05 '11 01:12

N Brouwer


People also ask

How do you truncate a value in a data frame?

Pandas DataFrame truncate() Method The truncate() method removes elements before and after the specified indexes or labels. Use the axis='columns' parameter to remove specified columns.

How do you truncate a string in Python?

Use string slicing to truncate a string Use the syntax string[x:y] to slice a string starting from index x up to but not including the character at index y . If index x is not specified it defaults to zero.

How do you truncate a value in a DataFrame Python?

truncate() function is used to truncate a Series or DataFrame before and after some index value. This is a useful shorthand for boolean indexing based on index values above or below certain thresholds.

What does output truncated mean?

To truncate is to shorten by cutting off. In computer terms, when information is truncated, it is ended abruptly at a certain spot. For example, if a program truncates a field containing the value of pi (3.14159265...) at four decimal places, the field would show 3.1415 as an answer.


1 Answers

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
like image 92
kohske Avatar answered Sep 29 '22 08:09

kohske