使用R函数lapply和read.sql.csv [英] Using R functions lapply and read.sql.csv

查看:327
本文介绍了使用R函数lapply和read.sql.csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用如下列表打开多个csv文件:

 文件名<  -  list.files (temp,pattern =*。csv,full.names = TRUE)

发现使用lapply和read.csv打开temp目录中的所有文件的示例,但我知道appriori什么数据我需要从文件中提取,所以为了节省时间阅读我想使用这个SQL扩展;

  somefile = read.csv.sql(temp / somefile.csv,sql =select * from file,eol = \ n)

但是我无法将这两个功能合并到一个命令我可以读取应用相同sql查询的目录中的所有文件。



有没有人成功了?

  files<  -  list.files(。,pattern =*。csv)
df.list< - sapply(filenames,read.csv.sql,sql =select * from file,eol =\\\
,simplify = F)

如果你想要他们所有组合:

  df < -  ldply(filenames,read.csv.sql,sql =select *从文件,eol =\\\


I am trying to open multiple csv files using a list such as the below;

filenames <- list.files("temp", pattern="*.csv", full.names=TRUE)

I have found examples that use lapply and read.csv to open all the files in the temp directory, but I know appriori what data i need to extract from the file, so to save time reading i want to use the SQL extension of this;

somefile = read.csv.sql("temp/somefile.csv", sql="select * from file ",eol="\n")

However i am having trouble combining these two pieces of functionality into a single command such that i can read all the files in a directory applying the same sql query.

Has anybody had success doing this?

解决方案

If you want a list of dataframes from each file (assuming your working directory contains the .csv files):

files <- list.files(".", pattern="*.csv")
df.list <- sapply(filenames, read.csv.sql,sql="select * from file ",eol="\n",simplify=F)

Or if you want them all combined:

df <- ldply(filenames, read.csv.sql,sql="select * from file ",eol="\n")

这篇关于使用R函数lapply和read.sql.csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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