R中的循环问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从哪里开始。 。 。
您可能需要为每个平均值、最大值和最小值设置 na.rm = TRUE,并且您需要为 sd 正确输入 TRUE。
hist(z) 应该位于 png(filename, ...) 之后,并且后面必须跟有 dev.off() (至少)。
在 R 中不能使用“+”将字符串粘贴在一起,请使用paste()。
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().