我试图在 for 循环中将两个胶水串粘贴在一起,但我没有得到想要的结果。例如,在复制示例中,我有两列,我想先循环第一列(一次)并在第二列的每个值上应用函数(x),依此类推,但是胶水代码运行第一列(一次又一次)-功能(第二列)。
到目前为止,我觉得这个问题令人困惑,希望下面的示例可以帮助澄清我的问题。
#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
-------------------------------------