R中的循环问题

发布于 2024-11-05 07:52:16 字数 688 浏览 4 评论 0原文

library(rgdal) 
my_asc = dir("~/Pulpit/dods/karol/TVDI113_121",
             pattern=".asc", recursive=TRUE, full.names=TRUE)
for (i in 1:length(my_asc)) {
    r <- readGDAL(my_asc[i])
    z <- as.matrix(r)
    vectordata[i] <- mean(z) 
    vectordatamax[i] <- max(z) 
    vectordatamin[i] <- min(z) 
    vectordev[i] <- sd(z, na.rm=TRUE) 
    hist(z)
    png(filename="hist"+tostring(i)+".png")
}

我尝试对此循环进行一些修改,但它仍然不起作用(我在 Rstudio 下工作) - 哪个片段不正确?

我还想使用更复杂的模式(仅列出末尾包含两个数字的文件),但添加如下内容: pattern="_??.asc" 似乎不起作用。

我想再添加一个循环来获取文件夹列表(而不是手动将目录插入 my_asc 变量),但我不知道如何做到这一点? 我不知道,为什么我为平均值、最大值、最小值和标准差值创建向量的方法不起作用......

library(rgdal) 
my_asc = dir("~/Pulpit/dods/karol/TVDI113_121",
             pattern=".asc", recursive=TRUE, full.names=TRUE)
for (i in 1:length(my_asc)) {
    r <- readGDAL(my_asc[i])
    z <- as.matrix(r)
    vectordata[i] <- mean(z) 
    vectordatamax[i] <- max(z) 
    vectordatamin[i] <- min(z) 
    vectordev[i] <- sd(z, na.rm=TRUE) 
    hist(z)
    png(filename="hist"+tostring(i)+".png")
}

I try to do some modification of this loop, but it still doesn't works (I am working under Rstudio) - which fragment is incorrect?

I would like also to use more complicated pattern (to list only files which contains on the end of it name two numbers), but adding something like:
pattern="_??.asc" seems not works.

I would like to add one more loop to get list of folders (instead of manually inserting directories into my_asc variable), but I haven't got Idea how I can do it?
I do not know, why my way of creating vectors for mean, max, min and standard deviation values is not working...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2024-11-12 07:52:16

从哪里开始。 。 。

您可能需要为每个平均值、最大值和最小值设置 na.rm = TRUE,并且您需要为 sd 正确输入 TRUE。

hist(z) 应该位于 png(filename, ...) 之后,并且后面必须跟有 dev.off() (至少)。

在 R 中不能使用“+”将字符串粘贴在一起,请使用paste()。

vectordata <- vectordatamax <- vectordatamin <- vectordev <- numeric(length(my_asc))
for (i in seq_along(my_asc)) {
  r <- readGDAL(my_asc[i])
  ## as.matrix is not necessary, as the band values are accessible directly
  ##z <- as.matrix(r)
  z <- r[[1]]
  vectordata[i] <- mean(z, na.rm=TRUE) 
  vectordatamax[i] <- max(z) 
  vectordatamin[i] <- min(z) 
  vectordev[i] <- sd(z, na.rm=TRUE) 
  png(filename=paste("hist", i, ".png", sep=""))
  hist(z)
  dev.off()
}

Where to start . . .

You probably want na.rm = TRUE for each of mean, max, and min, and you will need to enter TRUE correctly for sd.

The hist(z) should come after the png(filename, ...) and that will have to be followed by a dev.off() (at least).

You cannot paste together strings with "+" in R, use paste().

vectordata <- vectordatamax <- vectordatamin <- vectordev <- numeric(length(my_asc))
for (i in seq_along(my_asc)) {
  r <- readGDAL(my_asc[i])
  ## as.matrix is not necessary, as the band values are accessible directly
  ##z <- as.matrix(r)
  z <- r[[1]]
  vectordata[i] <- mean(z, na.rm=TRUE) 
  vectordatamax[i] <- max(z) 
  vectordatamin[i] <- min(z) 
  vectordev[i] <- sd(z, na.rm=TRUE) 
  png(filename=paste("hist", i, ".png", sep=""))
  hist(z)
  dev.off()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文