使用函数在文件名集合上重复函数

问题描述 投票:0回答:1

我想写一个以在中的文件名(工作目录中存在的文件名)集合上重复一段代码。如果可能,我还想将每行的输出保存为全局环境对象。下面是我要尝试做的一般结构。函数名称组成。

global_environment_object_1 <- anyfunction("filename and extension").
# Repeat this over a set of filenames in the working directory and save each as a separate 
# global environment object with separate names. 

一个现实生活中的例子可以是:

sds22 <- get_subdatasets("EVI_2017_June_2.hdf")
sds23 <- get_subdatasets("EVI_2016_June_1.hdf") 

-对象名和文件名正在更改,并且文件总数为48。

感谢您的事先帮助!

r function lapply sapply mapply
1个回答
0
投票

尝试使用:

#Get all the filenames
all_files <- list.files(full.names = TRUE)
#Probably to be specific
#all_files <- list.files(patter = "\\.hdf$", full.names = TRUE)
#apply get_subdatasets to each one of them
all_data <- lapply(all_files, get_subdatasets)
#Assign name to list output
names(all_data) <- paste0('sds', seq_along(all_data))
#Get the data in global environment
list2env(all_data, .GlobalEnv)
© www.soinside.com 2019 - 2024. All rights reserved.