sql - Read certain columns if exist using read.csv.sql from sqldf
问题描述
I have very large csv files (2.3 GB). I only want to read certain columns that could be or could not be there.
I am using the following code that was suggested here Only read limited number of columns
library(sqldf)
loc <- read.csv.sql("data.csv",
sql = "select locID, City, CRESTA, Latitude, Longitude from file",
sep = ",")
How can I deal with the situation when for example the column "City" is not in the csv?
解决方案
这会找出哪些列可用,将它们的名称与所需列的名称相交并仅读取这些列。
library(sqldf)
nms_wanted <- c("locID", "City", "CRESTA", "Latitude", "Longitude")
nms_avail <- names(read.csv("data.csv", nrows = 0))
nms <- intersect(nms_avail, nms_wanted)
fn$read.csv.sql("data.csv", "select `toString(nms)` from file")
推荐阅读
- istio - Istio queryParams 总是返回真值
- delphi - BitmapListAnimation - 如何跳转到选定的帧
- javascript - 当我通过它的 ID 调用它时,为什么我没有得到它的内容?
- scala - Spark 中毫秒格式的正确时间戳
- c# - 如何从我的对象中的另一个对象访问虚拟属性
- android - 第一次从 Android Studio 在真实设备中执行应用程序时出错
- azure-active-directory - 如何在两个不同 Azure 租户上运行的两个云应用程序之间启用 SSO?
- haskell - 如何使用 Pipes 库编写 GroupBy 函数
- angular - 如何从 Angular 中的 javascript 触发路由导航?
- c# - 启动时出现不同的错误 - SSIS Visual Studio