Home > Back-end >  How do we insert \n every n-character or/and after n-th space in a string in R?
How do we insert \n every n-character or/and after n-th space in a string in R?

Time:02-11

On SO I found a solution which helps to insert a value/character every n-th character in a string:

(?=(?:.{n}) $)

But it will be more reasonable to insert a value (for example, tabulation sign or \n) every n-th space, so the word won't be splitted. What are the possible ways to editing this regex?

I did my cluster analysis and now I want to attach labels to a dendrogram. Consider that the labels are very long strings like:

tibble(
   id = d2022_1,
   label = "A very long label for the dendro that should be splitted so it will look nicely in the picture"
) 

And I would like to have it tabulated/splited by rows, so I want to insert \n:

A very long label for the dendro\nthat should be splitted so\nit will look nicely in the picture

CodePudding user response:

You're reinventing the wheel here. R includes the strwrap function that can split a long string at appropriate word boundaries. This gives a more consistent line length than creating a break after n spaces.

For example, suppose I wanted a line break at most every 12 characters. I can do:

string <- "The big fat cat sat flat upon the mat"

strwrap(string, width = 12)
#> [1] "The big fat" "cat sat"     "flat upon"   "the mat" 

If you want newlines instead of split strings, just paste the result using collapse:

paste(strwrap(string, width = 12), collapse = "\n")
[1] "The big fat\ncat sat\nflat upon\nthe mat"

EDIT

Using the newly added example:

df <- tibble(
  id = "d2022_1",
  label = rep("A very long label for the dendro that should be splitted so it will look nicely in the picture", 2)
)

df
#> # A tibble: 2 x 2
#>   id      label                                                                        
#>   <chr>   <chr>                                                                        
#> 1 d2022_1 A very long label for the dendro that should be splitted so it will look nic~
#> 2 d2022_1 A very long label for the dendro that should be splitted so it will look nic~

df %>% mutate(label = sapply(label, function(x) paste(strwrap(x, 20), collapse = "\n")))
#> # A tibble: 2 x 2
#>   id      label                                                                        
#>   <chr>   <chr>                                                                        
#> 1 d2022_1 "A very long label\nfor the dendro that\nshould be splitted\nso it will look~
#> 2 d2022_1 "A very long label\nfor the dendro that\nshould be splitted\nso it will look~
  • Related