简体   繁体   English

循环遍历 R 中的 nc 文件

[英]Looping through nc files in R

Good morning everyone, I am currently using the code written by Antonio Olinto Avila-da-Silva on this link: https://oceancolor.gsfc.nasa.gov/forum/oceancolor/topic_show.pl?tid=5954 It allows me to extract data of type sst/chlor_a from nc file.大家早上好,我目前正在使用 Antonio Olinto Avila-da-Silva 在此链接上编写的代码: https://oceancolor.gsfc.nasa.gov/forum/oceancolor/topic_show.pl?tid=5954它允许我从 nc 文件中提取 sst/chlor_a 类型的数据。 It uses a loop to create an excel file with all the data.它使用循环创建包含所有数据的 excel 文件。 Unfortunately, I noticed that the function only takes the first data file in the loop.不幸的是,我注意到 function 只获取循环中的第一个数据文件。 Thus, I find myself with 20 times the same data in a row in my excel file.因此,我发现自己在 excel 文件中连续 20 倍于相同的数据。 Does anyone have a solution to make this loop work properly?有没有人有办法让这个循环正常工作?

I would first check out that these two lines contain all the files you are expecting:我将首先检查这两行是否包含您期望的所有文件:

(f <- list.files(".", pattern="*.L3m_MO_SST_sst_9km.nc",full.names=F))
(lf<-length(f))

And then there's a bug in the for-loop.然后在for循环中有一个错误。 This line:这一行:

data<-nc_open(f)

Needs to reference the iterator i , so change it to something like this:需要引用迭代器i ,所以把它改成这样:

data<-nc_open(f[[i]])

It appears both scripts have this same bug.看来这两个脚本都有同样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM