本文介绍了导入多个文件R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我希望你能帮助我。我一直在尝试将多个栅格数据集导入到R中,重新编码每个导入的栅格数据的缺失值,将它们链接到zipcodes,然后将每个栅格数据导出为具有不同名称的csv文件。 我试图使用循环,但到目前为止我只有错误。这是我已经得到的(不要笑): 导入所有24个栅格数据集 file_names = as.list(dir(pattern =home。*)) listy< -lapply(file_names,raster,band = 1) names ; - paste0(hour,seq_along(0:23)) 列表中的栅格数据集(listy),我只需要在每个列表上运行以下命令: listy的元素1的示例 listy $ hour1_r listy $ hour1_r [listy $ hour1_r == 9999]< -NA y&提取(listy $ hour1_r,zipcoords) hour1_zipcode< - cbind(zipid,y) write.table(hour1_zipcode,file =home\\urur1.csv,sep =, ,row.names = F) 如何使用循环? 我非常感谢您可能有任何建议! 谢谢! m 解决方案使用命令创建函数: f listy $ hour1_r listy $ hour1_r [listy $ hour1_r == 9999] ; -NA y hour1_zipcode write.table(hour1_zipcode,file = filename,sep = ,row.names = F)} 在所有元素上运行命令: / p> mapply(f,listy,filename = paste0(home\\hour,seq_along(listy),。csv )) 注意,你实际上不想要 mapply invisible(mapply(f,listy,filename = paste0(home\\urur,seq_along(listy),。csv))) 您也可以在for循环中执行此操作: filenames = paste0(home\\hour,seq_along (列表),。csv) for(i in seq_along(listy))f(listy [[i]],filenames [[i]]) / pre> I hope you can help me. I have been trying to import multiple raster datasets into R, recode missing values for each raster data imported, link them to zipcodes and then export each raster data as a csv file with different names.I am trying to use a loop but so far I have only got errors. This is as far as I have gotten (dont laugh):import all 24 raster datasetsfile_names=as.list(dir(pattern="home.*"))listy<-lapply(file_names,raster, band=1)names(listy) <- paste0("hour", seq_along(0:23))so now I have all the raster datasets in a list (listy) and I just need to run the following commands on each one:example for element 1 of listylisty$hour1_r<-listy$hour1listy$hour1_r[listy$hour1_r==9999]<-NAy <- extract(listy$hour1_r, zipcoords)hour1_zipcode <- cbind(zipid,y)write.table(hour1_zipcode,file="home\\hour1.csv",sep=",",row.names=F)How can I do this with a loop?I would very much appreciate any suggestions that you may have!Thank you!!!m 解决方案 Make a function out of your commands:f <- function(listy,filename) { listy$hour1_r<-listy$hour1 listy$hour1_r[listy$hour1_r==9999]<-NA y <- extract(listy$hour1_r, zipcoords) hour1_zipcode <- cbind(zipid,y) write.table(hour1_zipcode,file=filename,sep=",",row.names=F)}Run your command on all elements:mapply(f, listy, filename= paste0("home\\hour",seq_along(listy),".csv"))Note that you don't actually want mapply to return anything, so you could wrap it in invisible:invisible(mapply(f, listy, filename= paste0("home\\hour",seq_along(listy),".csv")))You could also do this in a for loop:filenames = paste0("home\\hour",seq_along(list),".csv")for (i in seq_along(listy)) f(listy[[i]],filenames[[i]]) 这篇关于导入多个文件R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS!
05-21 19:51