小编C.R*_*bin的帖子

print() 的输出在 R 中循环的每次迭代中替换自身

print()循环内调用的输出是否可以在循环的每次迭代中在控制台/交互式 Markdown 文件中替换自身?如果重要的话,我会在 RStudio 中完成所有 R 编码。

例如,这是我当前观察到的行为:

for (i in 1:20){
 print(paste('iteration', i)) 
}  

[1] "iteration 1"
[1] "iteration 2"
[1] "iteration 3"
[1] "iteration 4"
[1] "iteration 5"
[1] "iteration 6"
[1] "iteration 7"
[1] "iteration 8"
[1] "iteration 9"
[1] "iteration 10"
[1] "iteration 11"
[1] "iteration 12"
[1] "iteration 13"
[1] "iteration 14"
[1] "iteration 15"
[1] "iteration 16"
[1] "iteration 17"
[1] "iteration 18"
[1] "iteration 19"
[1] "iteration 20"

Run Code Online (Sandbox Code Playgroud)

对于相对较少的迭代次数,这并不是那么糟糕,但我确实喜欢打印出迭代,以便我可以跟踪它 - 特别是当有大量迭代时。那么有没有办法iteration …

for-loop r

4
推荐指数
1
解决办法
728
查看次数

提取函数内的列属性

假设我有以下内容:

df1 <- structure(list(var1 = structure(c("Didn't do a thing", "Almost did a thing", 
"Once did a thing", "Have never done a thing", "Always do a thing"
), description = "This is the question i asked respondents (and the title of the plot)"), 
    wtd_pct = c(4L, 15L, 62L, 11L, 8L)), row.names = c(NA, -5L
), class = c("tbl_df", "tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)

我想制作一个绘图函数,它将 tibble ( df1) 的名称和其中的列名称作为输入(在本例中,只有var1,但在我的实际 tibble 中我有更多列)。

在绘图函数内,我想提取连接到的属性var1并将其作为绘图标题。例如,在函数外部,如下所示:

df1 %>% 
  ggplot(aes(y = var1, x = wtd_pct)) …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 attr

1
推荐指数
1
解决办法
68
查看次数

标签 统计

r ×2

attr ×1

for-loop ×1

ggplot2 ×1