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

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

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

问题

Desired output:

1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse       
2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph  
4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew     
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个并逐个显示)。提前感谢您。

  • 数据:
dput(df)
structure(list(word = c("Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse", 
"Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron", 
"Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph", 
"Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew", 
"Jacob_Savannah_Nadia_Kaleem")), row.names = c(NA, -5L), class = c("tbl_df", 
"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:
&gt; head(dfLong, 10)
# A tibble: 5 x 1
  word                                                                           
  &lt;chr&gt;                                                                          
1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse       
2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph  
4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew     
5 Jacob_Savannah_Nadia_Kaleem    ### THIS IS WHAT I NEED TO CHANGE                                                
&gt; 
  • Desired output:
1 Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse       
2 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
3 Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph  
4 Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew     
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:
dput(df)
structure(list(word = c(&quot;Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse&quot;, 
&quot;Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron&quot;, 
&quot;Ramziyya_Marquez_Kiera_Farajallah_Larisa_Davier_Shujaa_Vincent_Orlando_Joseph&quot;, 
&quot;Desean_Chelsea_Faadil_Christopher_Aarifa_Joel_Matthew_Jacob_Aeones_Matthew&quot;, 
&quot;Jacob_Savannah_Nadia_Kaleem&quot;)), row.names = c(NA, -5L), class = c(&quot;tbl_df&quot;, 
&quot;tbl&quot;, &quot;data.frame&quot;))

答案1

得分: 2

这是您需要的吗?

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

数据:

df <- data.frame(
  word = c("Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse", 
           "Jacob_Savannah_Nadia_Kaleem",
           "Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron",
           "Ramziyya_Marquez_Shujaa_Vincent_Orlando_Joseph")
)
英文:

Is this what you need?

library(tidyverse)
df %&gt;%
  mutate(
    # count number of words:
    N = str_count(word, &quot;_&quot;) +1,
    # add placeholders:
  word = ifelse(N &lt; 10, 
                str_c(word, &quot;_&quot;, str_dup(&quot;&#39;&#39;_&quot;, 9 - N), &quot;&#39;&#39;&quot;), 
                word)
  ) %&gt;%
  select(-N) 
                                                                             word
1        Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
2                                   Jacob_Savannah_Nadia_Kaleem_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39;
3 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
4                      Ramziyya_Marquez_Shujaa_Vincent_Orlando_Joseph_&#39;&#39;_&#39;&#39;_&#39;&#39;_&#39;&#39; 

Data:

df &lt;- data.frame(
  word = c(&quot;Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse&quot;, 
           &quot;Jacob_Savannah_Nadia_Kaleem&quot;,
           &quot;Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron&quot;,
           &quot;Ramziyya_Marquez_Shujaa_Vincent_Orlando_Joseph&quot;)
)

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:

确定