R语言 粘贴胶水对象在一起运行循环

m2xkgtsf  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(91)

我试图在for循环中将两个胶水字符串粘贴在一起,但没有得到想要的结果。例如,在复制示例中,我有两列,我想先循环第一列(一次),然后对第二列的每个值应用function(x),依此类推,但胶水代码运行第一列(一次又一次)- function(第二列)。
到目前为止,我觉得这个问题令人困惑,希望下面的例子能帮助澄清我的问题。

#Reproduible example
#sample dataframe
col_A <- rep(c("one","two", "three", "four") ,each = 3)
col_B <- rep(c("yes", "No", "Maybe"),times = 4)

df <- bind_cols(a = col_A, b = col_B)

glucode_combined <- "" # Initialize empty string

# the loop over values to create a flexdashboard

for (i in unique(df$a)){

code_A <- glue(
    "{i} \n",
    "======================================================================= \n",
    )

code_B <- df %>% 
    filter(a == i) %>% 
    arrange(b) %>%
    glue_data(

"------------------------------------- \n",
"> ColumnA: {a} | ColumnB: {b} \n",
"------------------------------------- \n",
" \n",
    )

        
glucode_combined <- paste(glucode_combined, code_A, code_B, sep = "\n")
}

writeLines(glucode_combined,"glucode_combined.txt")

字符串
这将产生如下所示的结果(循环的第一部分反复重复

one 
======================================================================= 
------------------------------------- 
> ColumnA: one | ColumnB: Maybe 
------------------------------------- 
 
two 
======================================================================= 
------------------------------------- 
> ColumnA: two | ColumnB: Maybe 
------------------------------------- 
 
three 
======================================================================= 
------------------------------------- 
> ColumnA: three | ColumnB: Maybe 
------------------------------------- 
 
four 
======================================================================= 
------------------------------------- 
> ColumnA: four | ColumnB: Maybe 
------------------------------------- 
 

one 
======================================================================= 
------------------------------------- 
> ColumnA: one | ColumnB: No 
------------------------------------- 
 
two 
======================================================================= 
------------------------------------- 
> ColumnA: two | ColumnB: No 
------------------------------------- 
 
three 
======================================================================= 
------------------------------------- 
> ColumnA: three | ColumnB: No 
------------------------------------- 
 
four 
======================================================================= 
------------------------------------- 
> ColumnA: four | ColumnB: No 
-------------------------------------


然而我想产生像下面这样的结果,但我不知道我错过了什么

one 
======================================================================= 

------------------------------------- 
> ColumnA: one | ColumnB: yes 
------------------------------------- 

------------------------------------- 
> ColumnA: one | ColumnB: Maybe 
------------------------------------- 

------------------------------------- 
> ColumnA: one | ColumnB: No 
------------------------------------- 

 
two 
======================================================================= 

------------------------------------- 
> ColumnA: one | ColumnB: yes 
------------------------------------- 

------------------------------------- 
> ColumnA: one | ColumnB: Maybe 
------------------------------------- 

------------------------------------- 
> ColumnA: one | ColumnB: No 
-------------------------------------

2exbekwf

2exbekwf1#

问题是paste是矢量化的,而code_B是一个长度为3的矢量,所以paste尝试为code_B* 中的每个元素合并 * 组合字符串。看起来你并不想要这种行为,所以首先使用来自stringr库的类似str_flatten的东西来扁平化code_B

code_B <- df %>% 
    filter(a == i) %>% 
    arrange(b) %>%
    glue_data(
        "------------------------------------- \n",
        "> ColumnA: {a} | ColumnB: {b} \n",
        "------------------------------------- \n",
        " \n",
    ) %>%
    stringr::str_flatten()

字符串

bvhaajcl

bvhaajcl2#

尝试使用paste(collapse = "\n")而不是paste(sep = "\n")collapse将您的向量转换为一个字符串。

相关问题