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

siotufzp  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(121)

这是一个follow up question。我怎样才能在 _ 之间添加 n 个空字符串,使每行都有 * 正好 * 个单词,每个单词由 _ 分隔?

  • 数据为:
> 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                                                
>
  • 所需输出:
    问题
    1:我需要每行都有 exatcly 10个字。在单词数量不能被10整除的情况下,我想在_之间插入空字符串,像这样:_ ' ' _. tidyversestringr解决方案将不胜感激!谢谢!
    2:稍后,我还需要另一个df,其中所有行都合并到一个新单元格中。This answer帮助我。但是,在合并行之前,我需要确保每行正好有10个单词(稍后,我将使用Js split('_').slice(v,v+10).join("<br>"))在UI中显示此数据的每10个单词,我将以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"))
2nc8po8w

2nc8po8w1#

这是你需要的吗

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_''_''_''_''

数据:

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")
)

相关问题