Printing dataframes with long strings in R

前端 未结 3 1840
挽巷
挽巷 2021-02-20 00:13

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)),collaps         


        
3条回答
  •  攒了一身酷
    2021-02-20 00:34

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

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

提交回复
热议问题