英文:
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整除的情况下,我想在 _ 之间插入空字符串,就像这样:_ ' ' _。希望能提供 tidyverse 和 stringr 的解决方案!谢谢! <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:
 
> head(dfLong, 10)
# A tibble: 5 x 1
  word                                                                           
  <chr>                                                                          
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                                                
> 
- 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: 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('_').slice(v,v+10).join("<br>")), I'll split them by 10 and display one below the other). Thanks in advance.
- Data:
 
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"))
答案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 %>%
  mutate(
    # count number of words:
    N = str_count(word, "_") +1,
    # add placeholders:
  word = ifelse(N < 10, 
                str_c(word, "_", str_dup("''_", 9 - N), "''"), 
                word)
  ) %>%
  select(-N) 
                                                                             word
1        Jason_Oscar_Maleeka_Janet_Gabriel_Raheema_Bryce_Nasreen_Hishaam_Thadduse
2                                   Jacob_Savannah_Nadia_Kaleem_''_''_''_''_''_''
3 Marcos_Daijah_Chassity_Carlito_Chidiebere_Matthew_Maureene_Jillian_Markus_Aaron
4                      Ramziyya_Marquez_Shujaa_Vincent_Orlando_Joseph_''_''_''_'' 
Data:
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")
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论