目前,我正在使用下面的代码一次将~7-10个文件读入R控制台。
library(magrittr)
library(feather)
list.files("C:/path/to/files",pattern="\\.feather$") %>% lapply(read_feather)如何根据它们的唯一文件名将它们导入到独立的数据对象中?
例如。
accounts_jan.feather
users_jan.feather
-> read feather function -> hold in working memory as:
accounts_jan_df
users_jan_df谢谢。
发布于 2017-02-01 05:21:26
这看起来像是试图用管道(https://github.com/hrbrmstr/rstudioconf2017/blob/master/presentation/writing-readable-code-with-pipes.key.pdf)做太多的事情。我建议将您的流程稍微细分一下:
# Get vector of files
files <- list.files("C:/path/to/files", pattern = "\\.feather$")
# Form object names
object_names <-
files %>%
basename %>%
file_path_sans_ext
# Read files and add to environment
lapply(files,
read_feather) %>%
setNames(object_names) %>%
list2env()如果您确实必须使用单个管道执行此操作,则应该使用mapply,因为它有一个USE.NAMES参数。
list.files("C:/path/to/files", pattern = "\\feather$") %>%
mapply(read_feather,
.,
USE.NAMES = TRUE,
SIMPLIFY = FALSE) %>%
setNames(names(.) %>% basename %>% tools::file_path_sans_ext) %>%
list2env()就我个人而言,当我去做调试时,我发现第一种选择更容易理解(我不是管道中管道的粉丝)。
https://stackoverflow.com/questions/41966873
复制相似问题