我们如何在 R 中的字符串中的第 n 个 space 之后插入 \n 每个 n 个字符 or/and?

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

在 SO 上,我找到了一个解决方案,它有助于在字符串中每第 n 个字符插入一个 value/character:

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

但是每第n个space插入一个值(例如制表符或\n)会更合理,所以单词不会被拆分。编辑此正则表达式的可能方法是什么?

我进行了聚类分析,现在我想将标签附加到树状图。考虑到标签是很长的字符串,例如:

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

我想按行 tabulated/splited,所以我想插入 \n:

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

您正在重新发明轮子。 R 包含 strwrap 函数,可以在适当的单词边界处拆分长字符串。这比在 n 个空格后创建一个中断提供了更一致的行长度。

例如,假设我最多希望每 12 个字符有一个换行符。我能做到:

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

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

如果您想要换行而不是拆分字符串,只需使用折叠粘贴结果:

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

编辑

使用新添加的示例:

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~