如何插入字符串以使每一行具有相同的模式

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

How to insert strings so that every row has the same pattern

问题

Desired output:

  1. 1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
  2. 2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
  3. 3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph
  4. 4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew
  5. 5 Jacob_Savannah_Nadia_Kaleem_''_''_''_''_''_'' ## LIKE THIS

Questions:

1: 我需要每一行都有 exatcly 10 个单词。在单词数量不能被10整除的情况下,我想在 _ 之间插入空字符串,就像这样:_ ' ' _。希望能提供 tidyversestringr 的解决方案!谢谢! <br>

2: 之后,我还需要另一个包含所有行合并成一个新单元格的数据框。这个答案 帮助了我。但在合并行之前,我需要确保每行恰好有10个单词(之后,我将使用 Js 的 split(' ').slice(v,v+10).join("<br>") 将它们分割成10个并逐个显示)。提前感谢您。

  • 数据:
  1. dput(df)
  2. structure(list(word = c("Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse",
  3. "Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron",
  4. "Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph",
  5. "Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew",
  6. "Jacob_Savannah_Nadia_Kaleem")), row.names = c(NA, -5L), class = c("tbl_df",
  7. "tbl", "data.frame"))
英文:

This is a follow up question. How can I add n empty strings between _ so that every row has exactly 10 words separated by _ each?

  • The data is:
  1. &gt; head(dfLong, 10)
  2. # A tibble: 5 x 1
  3. word
  4. &lt;chr&gt;
  5. 1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
  6. 2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
  7. 3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph
  8. 4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew
  9. 5 Jacob_Savannah_Nadia_Kaleem ### THIS IS WHAT I NEED TO CHANGE
  10. &gt;
  • Desired output:
  1. 1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
  2. 2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
  3. 3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph
  4. 4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew
  5. 5 Jacob_Savannah_Nadia_Kaleem_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39; ## LIKE THIS

Questions:

1: I need EVERY row to have exatcly 10 words each. In cases in which the amount of words is not exactly divisible by 10, I want to insert empty strings between _, like this: _ ' ' _. tidyverse and stringr solutions would be much appreciated! Thanks! <br>

2: Later on, I also need another df in which ALL rows are combined into a new cell. This answer helped me. But, before merging the rows, I need to make sure that I have exactly 10 words per row (later on, I'll be displaying every 10 words of this data in a UI using Js split(&#39;_&#39;).slice(v,v+10).join(&quot;&lt;br&gt;&quot;)), I'll split them by 10 and display one below the other). Thanks in advance.

  • Data:
  1. dput(df)
  2. structure(list(word = c(&quot;Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse&quot;,
  3. &quot;Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron&quot;,
  4. &quot;Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph&quot;,
  5. &quot;Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew&quot;,
  6. &quot;Jacob_Savannah_Nadia_Kaleem&quot;)), row.names = c(NA, -5L), class = c(&quot;tbl_df&quot;,
  7. &quot;tbl&quot;, &quot;data.frame&quot;))

答案1

得分: 2

这是您需要的吗?

  1. library(tidyverse)
  2. df %>%
  3. mutate(
  4. # 计算单词数:
  5. N = str_count(word, "_") + 1,
  6. # 添加占位符:
  7. word = ifelse(N < 10,
  8. str_c(word, "_", str_dup("''_", 9 - N), "''"),
  9. word)
  10. ) %>%
  11. select(-N)

数据:

  1. df <- data.frame(
  2. word = c("Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse",
  3. "Jacob_Savannah_Nadia_Kaleem",
  4. "Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron",
  5. "Ramziyya_Marquez_Shujaa_Vincent_Orlando_Joseph")
  6. )
英文:

Is this what you need?

  1. library(tidyverse)
  2. df %&gt;%
  3. mutate(
  4. # count number of words:
  5. N = str_count(word, &quot;_&quot;) +1,
  6. # add placeholders:
  7. word = ifelse(N &lt; 10,
  8. str_c(word, &quot;_&quot;, str_dup(&quot;&#39;&#39;_&quot;, 9 - N), &quot;&#39;&#39;&quot;),
  9. word)
  10. ) %&gt;%
  11. select(-N)
  12. word
  13. 1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
  14. 2 Jacob_Savannah_Nadia_Kaleem_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;
  15. 3 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
  16. 4 Ramziyya_Marquez_Shujaa_Vincent_Orlando_Joseph_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;

Data:

  1. df &lt;- data.frame(
  2. word = c(&quot;Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse&quot;,
  3. &quot;Jacob_Savannah_Nadia_Kaleem&quot;,
  4. &quot;Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron&quot;,
  5. &quot;Ramziyya_Marquez_Shujaa_Vincent_Orlando_Joseph&quot;)
  6. )

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

发表评论

匿名网友

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

确定