在R中打印带有长字符串的数据帧

时间:2022-12-14 18:23:43

Let's have a dataframe with long strings in one column:

让我们在一列中有一个包含长字符串的数据帧:

 df<-data.frame(short=rnorm(10,0,1),long=replicate(10,paste(rep(sample(letters),runif(1,5,8)),collapse="")))

How could I print the dataframe without showing the entire string? Something like this:

如何在不显示整个字符串的情况下打印数据帧?像这样的东西:

        short        long
1   0.2492880 ghtaprfv...
2   1.0168434 zrbjxvci...
3   0.2460422 yaghkdul...
4   0.1741522 zuabgxpt...
5  -1.1344230 mzhjtwcr...
6  -0.7104683 fcbhuegt...
7   0.2749227 aqyezhbl...
8  -0.4395554 azecsbnk...
9   2.2837716 lkgwzedf...
10  0.7695538 omiewuyn...

3 个解决方案

#1


7  

You can redefine the print.data.frame method, and in this function use substr to trim your character vectors to the desired maximum length:

您可以重新定义print.data.frame方法,并在此函数中使用substr将您的字符向量修剪为所需的最大长度:

print.data.frame <- function (x, ..., maxchar=20, digits = NULL, quote = FALSE,
    right = TRUE, row.names = TRUE) 
{
  x <- as.data.frame(
      lapply(x, function(xx)
            if(is.character(xx)) substr(xx, 1, maxchar) else xx)
  )
  base::print.data.frame(x, ..., digits=digits, quote=quote, right=right, 
      row.names=row.names)
}

Create data. Note my addition of stringsAsFactors=FALSE:

创建数据。注意我添加了stringsAsFactors = FALSE:

df <- data.frame(
    short=rnorm(10,0,1),
    long=replicate(10,paste(rep(sample(letters),runif(1,5,8)),collapse="")),
    stringsAsFactors=FALSE
)

Print data.frame:

打印data.frame:

print(df, maxchar=10)
        short       long
1  -0.6188273 cpfhnjmeiw
2  -0.0570548 bwcmpinedr
3  -0.5795637 dcevnyihlj
4   0.1977156 qzxlhvnarm
5  -1.9551196 aiflwtkjdq
6  -1.2429173 vlscerwhgq
7  -0.5897045 fziogkpsyr
8   0.4946985 pdeswloxcn
9   0.3262543 kxlofchszd
10 -1.8059621 wncaedpzty

#2


2  

This is one way:

这是一种方式:

within(df, {
    long = paste(substr(long, 1, 10), "...", sep = "")
})

I use substr to get the first part of the string, than I use paste for the "...". To permanently change the characters in df, simply do:

我使用substr来获取字符串的第一部分,而不是使用paste作为“...”。要永久更改df中的字符,只需执行以下操作:

df = within(df, {
    long = paste(substr(long, 1, 10), "...", sep = "")
})

#3


0  

Uses dplyr and prints out a modified version of the original data frame (without changing it). Only shortens values which exceed specified length:

使用dplyr并打印出原始数据框的修改版本(不更改它)。只缩短超过指定长度的值:

library(dplyr)

print.data.frame(df %>% mutate(long = ifelse(
    nchar(long > 11),
    paste0(substr(long, 1, 8), "..."),
    long
)))

#1


7  

You can redefine the print.data.frame method, and in this function use substr to trim your character vectors to the desired maximum length:

您可以重新定义print.data.frame方法,并在此函数中使用substr将您的字符向量修剪为所需的最大长度:

print.data.frame <- function (x, ..., maxchar=20, digits = NULL, quote = FALSE,
    right = TRUE, row.names = TRUE) 
{
  x <- as.data.frame(
      lapply(x, function(xx)
            if(is.character(xx)) substr(xx, 1, maxchar) else xx)
  )
  base::print.data.frame(x, ..., digits=digits, quote=quote, right=right, 
      row.names=row.names)
}

Create data. Note my addition of stringsAsFactors=FALSE:

创建数据。注意我添加了stringsAsFactors = FALSE:

df <- data.frame(
    short=rnorm(10,0,1),
    long=replicate(10,paste(rep(sample(letters),runif(1,5,8)),collapse="")),
    stringsAsFactors=FALSE
)

Print data.frame:

打印data.frame:

print(df, maxchar=10)
        short       long
1  -0.6188273 cpfhnjmeiw
2  -0.0570548 bwcmpinedr
3  -0.5795637 dcevnyihlj
4   0.1977156 qzxlhvnarm
5  -1.9551196 aiflwtkjdq
6  -1.2429173 vlscerwhgq
7  -0.5897045 fziogkpsyr
8   0.4946985 pdeswloxcn
9   0.3262543 kxlofchszd
10 -1.8059621 wncaedpzty

#2


2  

This is one way:

这是一种方式:

within(df, {
    long = paste(substr(long, 1, 10), "...", sep = "")
})

I use substr to get the first part of the string, than I use paste for the "...". To permanently change the characters in df, simply do:

我使用substr来获取字符串的第一部分,而不是使用paste作为“...”。要永久更改df中的字符,只需执行以下操作:

df = within(df, {
    long = paste(substr(long, 1, 10), "...", sep = "")
})

#3


0  

Uses dplyr and prints out a modified version of the original data frame (without changing it). Only shortens values which exceed specified length:

使用dplyr并打印出原始数据框的修改版本(不更改它)。只缩短超过指定长度的值:

library(dplyr)

print.data.frame(df %>% mutate(long = ifelse(
    nchar(long > 11),
    paste0(substr(long, 1, 8), "..."),
    long
)))