r - 将胶水对象粘贴在一起循环运行
问题描述
我试图在 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
-------------------------------------
解决方案
问题在于它paste
是向量化的,并且code_B
是一个长度为 3 的向量,因此 paste 尝试组合code_B 中每个元素的字符串。看起来你不想要这种行为,所以首先使用图书馆中的code_B
东西str_flatten
来展平:stringr
code_B <- df %>%
filter(a == i) %>%
arrange(b) %>%
glue_data(
"------------------------------------- \n",
"> ColumnA: {a} | ColumnB: {b} \n",
"------------------------------------- \n",
" \n",
) %>%
stringr::str_flatten()
推荐阅读
- javascript - 如何隐藏一个div下面的所有div元素?
- windows - 无法 RDP 进入 Windows?
- typescript - Nuxt 自定义插件导出未定义
- kubernetes - Google 的 Cloud Run 与传统的 Kubernetes 集群有何不同?
- java - 如何序列化 BCMcEliecePublicKey?
- python - 如何将 Counter 的结果放入两个列表中?
- android - findViewById() 从自定义视图返回 null
- excel - 复制粘贴宏的工作方式因活动工作表而异
- python - 从 BeautifulSoup Python 获取 CDATA
- java - 无法在 EditText 字段中发送密钥