r - 在for循环中生成序列

标签 r loops for-loop

这可能已得到解答,但我找不到针对我的具体问题的解决方案: 我正在寻找某种文件类型(例如 jpeg)并使用 message() 函数向用户打印 message 。但除了消息之外,我还想打印一个序列。 输出应为:

1. Found filename at System time.
2. Found filename2 at System time. 

不幸的是,我的输出是:

1 Found filename at System time
2 Found filename at System time
1 Found filename2 at System time
2 Found filename2 at System time

我的代码:

library(tools)
setwd("/Users/RLearner/Desktop/TEMP")
a<-list.files(getwd(), recursive=TRUE)
for (f in a)
  for (i in 1:length(a))
    if (file_ext(f)=="jpeg")
    {
      message(paste(i, "Found", f, "-", Sys.time(), sep = " "))
    }

我做错了什么?

最佳答案

以下还不够吗?

setwd("/Users/RLearner/Desktop/TEMP")
a <- list.files(getwd(), pattern="*jpeg$", recursive=TRUE)

out <- paste(seq_along(a), "Found", "-", a, Sys.time(), sep = " ")

编辑

我无法通过简单地使用 out 提供 message 来找到解决方案。仍然需要 for 循环。如果有人能找到包装,请随时编辑。

for (ii in out){
  message(ii)
}

关于r - 在for循环中生成序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33009787/

相关文章:

c - 如何将for循环转换为while循环?

algorithm - MATLAB 中数字的负数

python - 为什么变量在循环中调用后会发生变化?

php - 循环代码优化

java - 我对代码段的运行时分析是否正确?

matlab - 在 matlab for loop 中测试迭代值失败?

arrays - 函数类似于Matlab中的head()

r - 在 R 中优化带有约束的曲线组合

logging - R 有标准的日志包吗?

r - 将 purrr 和 ggplot 与 group_by 和 Nest() 结合使用时获取绘图标题