如何使用apply函数将多个数据表读入R中? [英] How to read in multiple data tables into R using apply function?

查看:70
本文介绍了如何使用apply函数将多个数据表读入R中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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

任何人都可以提供任何建议吗?

Can anyone offer any advice?

推荐答案

根据您希望输出的内容,有以下几种选择:

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))

将多个数据框作为不同的对象加载到Global Environment

# 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆