Using R functions lapply and read.sql.csv

荒凉一梦 提交于 2019-12-22 11:22:57

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/15134327/using-r-functions-lapply-and-read-sql-csv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!