如何在R中将行的值包装到特定字符处的新行

huangapple go评论105阅读模式
英文:

How to wrap values of a row into new line at specific character in R

问题

以下是您要翻译的内容:

  1. 我有一个带有数值的示例数据框:
  2. ```R
  3. data <- structure(list(A = c("Date)", "Values"), B = c("2023-04-03", "Heat Capacity\nSpecific Heat Capacity\nHeat Index" )), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame" ))

这里最后一列的第二行延伸到右边。我希望每个值都在新的一行,而不添加新的行。

我的期望输出:

[![enter image description here][1]][1]

上面的表格是使用gt()创建的:

  1. library(gt)
  2. data %>%
  3. mutate(b = str_replace_all(b, "\n", "<br>")) %>%
  4. gt() %>%
  5. fmt_markdown(columns = TRUE)

但这对于我最终期望的输出不起作用,我想要的是一个xlsx文件,使用gt()是行不通的。
是否可以使用dplyrstringr来实现类似的结果?

  1. 请注意,我已经按您的要求只翻译了代码部分,不包括其他内容。
  2. <details>
  3. <summary>英文:</summary>
  4. I have a sample dataframe with values:

data <- structure(list(A = c("Date)", "Values"), B = c("2023-04-03", "Heat Capacity\nSpecific Heat Capacity\nHeat Index" )), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame" ))

  1. Here the 2nd row of last column extends to the right. I want the each values to be in new line without adding new rows.
  2. My desired output:
  3. [![enter image description here][1]][1]
  4. The above table is created using `gt()`:

library(gt)
data %>%
mutate(b = str_replace_all(b, "\n", "<br>")) %>%
gt() %>%
fmt_markdown(columns = TRUE)

  1. But this is not useful as my final desired output I want is in xlsx file and using `gt()` wouldn&#39;t work.
  2. Is it possible to do anything with `dplyr` and `stringr` to achieve this similar result
  3. [1]: https://i.stack.imgur.com/oZVrv.png
  4. </details>
  5. # 答案1
  6. **得分**: 1
  7. 你可以使用`gt`包来格式化高质量的表格演示。查看[这里的`gt`介绍](https://gt.rstudio.com/articles/intro-creating-gt-tables.html)。
  8. 以下是代码,输出已省略。
  9. ``` r
  10. suppressPackageStartupMessages({
  11. library(tidyverse)
  12. library(gt)
  13. })
  14. data <- structure(list(
  15. A = c("日期", "数值"),
  16. B = c("2023-04-03", "热容\n比热容\n热指数")),
  17. row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"))
  18. data %>%
  19. mutate(B = gsub("\n", "<br>", B)) %>%
  20. gt() %>%
  21. fmt_markdown(columns = everything())

创建于2023-08-05,使用reprex v2.0.2


编辑

你可以将其导出为HTML文件,然后在Excel中打开:

  1. data %>%
  2. mutate(B = gsub("\n", "<br>", B)) %>%
  3. gt() %>%
  4. fmt_markdown(columns = everything()) %>%
  5. as_raw_html() %>%
  6. writeLines(con = "~/Temp/so.html")
英文:

You can use package gt to format presentation quality tables.
See this introduction to gt.

Here is the code, output omitted.

  1. suppressPackageStartupMessages({
  2. library(tidyverse)
  3. library(gt)
  4. })
  5. data &lt;- structure(list(
  6. A = c(&quot;Date)&quot;, &quot;Values&quot;),
  7. B = c(&quot;2023-04-03&quot;, &quot;Heat Capacity\nSpecific Heat Capacity\nHeat Index&quot; )),
  8. row.names = c(NA, -2L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot; ))
  9. data %&gt;%
  10. mutate(B = gsub(&quot;\\n&quot;, &quot;&lt;br&gt;&quot;, B)) %&gt;%
  11. gt() %&gt;%
  12. fmt_markdown(columns = everything())

<sup>Created on 2023-08-05 with reprex v2.0.2</sup>


Edit

You can export to a HTML file and open the file in Excel:

  1. data %&gt;%
  2. mutate(B = gsub(&quot;\\n&quot;, &quot;&lt;br&gt;&quot;, B)) %&gt;%
  3. gt() %&gt;%
  4. fmt_markdown(columns = everything()) %&gt;%
  5. as_raw_html() %&gt;%
  6. writeLines(con = &quot;~/Temp/so.html&quot;)

huangapple
  • 本文由 发表于 2023年8月5日 07:19:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定