【问题标题】:loop to open, get variables and export multiple nc files in r循环打开,获取变量并在 r 中导出多个 nc 文件
【发布时间】:2021-05-08 23:14:45
【问题描述】:

我有超过 60 个特定坐标的 nc 文件,其中仅包含一个变量。我想从 csv 文件中提取变量(tasmax)。 一个文件的代码是:

ncdata <- nc_open("data.nc")
tasmax<-ncvar_get(ncdata,"tasmax")
nc_close(ncdata)
tasmax<-as.data.frame(tasmax)
write.csv2(tasmax,file="data.csv")

我试图为所有文件创建一个循环,但总是出错..有什么想法吗?

【问题讨论】:

    标签: r loops netcdf netcdf4 ncdf4


    【解决方案1】:

    我遵循此代码并工作:

    df=NULL
    
    files<- list.files('tasmax/ssp370',pattern='*.nc',full.names=TRUE)
    
    for(i in seq_along(files)) {
     nc = nc_open(files[i])code 
    tasmax<-ncvar_get(nc,"tasmax")
    nc_close(nc)
    cbind(df,tasmax)->df
    }
    df<-as.data.frame(df)
    
    write.csv2(df,file="data.csv")
    

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      相关资源
      最近更新 更多