【发布时间】:2021-01-26 18:03:57
【问题描述】:
我有以下空数据框/tibble:
new_table <- tibble(
country = character(),
code = character()
)
创建 0 行 x 2 列后为空
我有以下代码数据框:
df_codes <- tibble(codes = c('CH','US','UK'))
看起来像这样:
codes
-----
CH
US
UK
有没有办法循环遍历 df_codes 数据帧中的每个元素并将这些值插入到我的 new_table 数据帧代码列中?
我尝试了以下代码,但无济于事:
for(c in unique(df_codes$codes)){
new_table <- new_table %>% mutate(code = c)
return(new_table)
}
但这仍然返回一个 0 行 2 列的数据框:
理想情况下,我希望在调用 new_table 时得到这个输出:
country|code
-------|-----
NA | CH
NA | US
NA | UK
感谢任何帮助
【问题讨论】:
-
您在第一次通过循环后立即返回(从哪里来?)。
标签: r dataframe dplyr tidyverse