1

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!

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"))
Larissa Cury
  • 806
  • 2
  • 11

1 Answers1

2

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")
)
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
  • Yes, it is! Thank you! Would you mind explaning the logic behind the if/else statment? – Larissa Cury May 13 '23 at 18:31
  • 1
    Sure: `ifelse`takes three argments: the condition to be met (here: `N < 10`, i.e., `N` is smaller than 10), what to do if the the condition returns TRUE (here: string together (i) the vector `word`, (ii) an underscore `_`, the string `''_`9 times - N), and a concluding `''`, and (iii) what to do if the condition returns FALSE (here: just just `word` as is). Does this clarify things or do you need more clarification? – Chris Ruehlemann May 13 '23 at 20:28