如果我有以下文件夹结构...
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 project.RProj\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 some_data.csv\n\xe2\x94\x82\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 notebooks\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 analysis.Rmd\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output\nRun Code Online (Sandbox Code Playgroud)\n使用 knit 按钮时,如何更改 RMarkdown 文件中的 yaml 以将 HTML 文件输出到输出文件夹而不是笔记本文件夹中?
\n我希望能够在 RMarkdown 中单击 knit 并最终得到......
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 project.RProj\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 some_data.csv\n\xe2\x94\x82\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 notebooks\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 analysis.Rmd\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 analysis.html\nRun Code Online (Sandbox Code Playgroud)\n我已经看到 Yihui 有一个很好的示例,说明如何编辑输出以在 R Markdown 食谱中添加日期(https://bookdown.org/yihui/rmarkdown-cookbook/custom-knit.html)。贴在下面。但我想为这个特定用例提供一个示例。
\nknit: (function(input, ...) {\n rmarkdown::render(\n input,\n output_file = paste0(\n xfun::sans_ext(input), \'-\', Sys.Date(), \'.html\'\n ),\n envir = globalenv()\n )\n })\nRun Code Online (Sandbox Code Playgroud)\n 我有一个小...
# A tibble: 20 x 6
id X_1 Y_1 number X_2 Y_2
<int> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 1 3 1 1 3
2 1 1 3 0 1 3
3 2 2 4 1 2 4
4 2 2 4 0 2 4
5 3 1 3 1 1 3
6 3 1 3 0 1 3
Run Code Online (Sandbox Code Playgroud)
如果数字列中的值等于 1,我想让所有值都等于 NA,但仅在以“_1”结尾的列中(所以 X_1 和 Y_1)。
我还想在 _2 列中做相反的事情(即数字等于零的行变为 NA)。
它应该最终看起来像这样......
# A tibble: 20 x 6
id X_1 …Run Code Online (Sandbox Code Playgroud) 假设我有以下数据框......
# Starting dataframe
data <- tribble(
~ID, ~Excluded, ~colA, ~colB, ~colC, ~col_mean, ~varA, ~varB,
"A", TRUE, 1, 1, 1, 1, "X", 10,
"B", FALSE, NA, 2, 2, NA, "Y", 20,
"C", FALSE, 3, 3, 3, 3, "Z", 30
)
Run Code Online (Sandbox Code Playgroud)
以及一个子集数据框(即存在较少的观察值),其中缺失值已被估算,例如......
# Dataframe with imputed values
data_imputed <- tribble(
~ID, ~Excluded, ~colA, ~colB, ~colC, ~col_mean, ~varA, ~varB,
"B", FALSE, 2, 2, 2, 2, "Y", 20,
"C", FALSE, 3, 3, 3, 3, "Z", 30
)
Run Code Online (Sandbox Code Playgroud)
当特定列(例如col_mean)缺少值时,如何用估算数据框中的值替换原始数据框中的值?
注意:我不想用估算数据帧中的行替换整行,而只是替换一组指定的列(例如,在本例中,以“col”开头的列)。
目标数据框看起来像这样......
# …Run Code Online (Sandbox Code Playgroud)