如何使用apply函数将多个数据表读入R? [英] How to read in multiple data tables into R using apply function?
问题描述
我对 R 比较陌生,我在使用 apply 函数从目录中读取多个表时遇到问题.我想让该函数做的是使用一个带有指向我感兴趣的表的路径的向量,并生成一个列表,其中包含与该文件中的路径相对应的每个数据框的对象.我编写了以下代码:
I am relatively new to R and I'm having a problem reading in multiple tables from a directory using an apply function. What I would like to have the function do is to use a vector with paths to tables that I'm interested in and generate a list with objects consisting of each data frame corresponding the paths in that file. I've written the following code:
f<- function(directory){
file.list <<- list.files(directory)
file.paths <<- as.vector(paste(directory, file.list, sep = "/"))
tables <- lapply(X = file.paths, FUN = read.table, header = TRUE,sep = "\t" ))
}
据我所知,我正在做的是在我想要的目录中创建一个文件名列表,创建这些文件的路径,并且(我失败的地方是)遍历这些路径并导入表它们对应于整个 file.paths 对象并生成包含这些表的列表.我收到以下错误:
By my understanding, what I'm doing is creating a list of file names in the directory that I want, creating a path to those files, and (where I'm failing is) looping over those paths and importing the tables they correspond to for the whole file.paths object and generating a list with those tables. I receive the following error:
Error in FUN(X[[i]], ...) : no lines available in input
谁能给点建议?
推荐答案
以下是一些选项,具体取决于您希望输出的内容:
Here are a few options depending on what you want the output to be:
数据框列表
# Load library
library(data.table)
# Get a List of all files named with a key word, say all `.csv` files
filenames <- list.files("C:/your/folder", pattern="*.csv", full.names=TRUE)
# Load data sets
list.DFs <- lapply(filenames,fread)
我假设您的数据文件以 .csv
格式保存.请注意,fread
等价于 read.table
但要快得多
I'm assuming your data files are saved in .csv
format. Note that fread
is equivalent to read.table
but much much faster
将多个数据框绑定为一个数据框
# Get a List of all files named with a key word, say all `.csv` files
filenames <- list.files("C:/your/folder", pattern="*.csv", full.names=TRUE)
# Load and bind all data sets
data <- rbindlist(lapply(filenames,fread))
将多个数据框作为不同对象加载到全局环境
# Get a List of DF in the directory
filenames <- list.files("C:/your/folder", pattern="*.Rda", full.names=TRUE)
# Load data sets
lapply(filenames, load, .GlobalEnv)
这篇关于如何使用apply函数将多个数据表读入R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!