14

我有一个使用以下函数读取的 csv 文件:

csvData <- read.csv(file="pf.csv", colClasses=c(NA, NA,"NULL",NA,"NULL",NA,"NULL","NULL","NULL"))
dimnames(csvData)[[2]]<- c("portfolio", "date", "ticker", "quantity")

它从该文件中读取所有行。但我想从阅读中跳过一些行。ticker如果-column 的值为:ABT或,则不应读取该行ADCT。可能吗?

我的 csv 文件示例如下:

RUS1000,01/29/1999,21st Centy Ins Group,TW.Z,90130N10,72096,1527.534,0.01,21.188
RUS1000,01/29/1999,3com Corp,COMS,88553510,358764,16861.908,0.16,47.000
RUS1000,01/29/1999,3m Co,MMM,88579Y10,401346,31154.482,0.29,77.625
RUS1000,01/29/1999,A D C Telecommunicat,ADCT,00088630,135114,5379.226,0.05,39.813
RUS1000,01/29/1999,Abbott Labs,ABT,00282410,1517621,70474.523,0.66,46.438
RUS1000,02/26/1999,21st Centy Ins Group,TW.Z,90130N10,72096,1378.836,0.01,19.125
RUS1000,02/26/1999,3com Corp,COMS,88553510,358764,11278.644,0.11,31.438
RUS1000,02/26/1999,3m Co,MMM,88579Y10,402146,29783.938,0.29,74.063 
4

4 回答 4

25

可以使用sqldf 包,使用read.csv.sql

让我们说sample.csv看起来像这样的内容:

id,name,age
1,"a",23
2,"b",24
3,"c",23

现在只读取 age=23 的行:

require(sqldf)

df <- read.csv.sql("sample.csv", "select * from file where age=23")

df
  id name age
1  1  "a"  23
2  3  "c"  23

可以选择必要的列:

df <- read.csv.sql("sample.csv", "select id, name from file where age=23")
df
  id name
1  1  "a"
2  3  "c"
于 2013-04-12T09:15:04.877 回答
2

最好像评论中建议的那样稍后阅读所有内容和子集:

csvData [!csvData$ticker %in% c('ADCT','ABT'),]

编辑

您可以使用freadfrom data.tablepackage 更有效的方法来读取您的文件。

library(read.table)
fread(file="pf.csv")
于 2013-04-12T09:13:04.293 回答
0

对我来说,sqldf 包的 read.csv.sql 乍一看很棒。但是当我尝试使用它时,它无法处理“NULL”字符串。(其他人也发现了这一点。)不幸的是,它不支持所有 read.csv 功能。所以我不得不自己写。我很惊讶没有一个好的包。

fetchLines=function(inputFile,match,fixed=T,n=100,maxlines=100000){ #inputFile='simple.csv'; match='APPLE';
  message('reading:',inputFile)
  n=min(n,maxlines)
  con  <- base::file(inputFile, open = "r",encoding = "UTF-8-BOM")
  data=c(readLines(con, n = 1, warn = FALSE))
  while (length(oneLine <- readLines(con, n = n, warn = FALSE)) > 0) {
    grab=grep(match,oneLine,value=T,fixed=fixed)
    if(length(grab)>0){
      data=c(data,grab)
      if(length(data)>maxlines){
        warning("bailing out too many");
        return(data);
      }
      cat('.')
    }
  } 
  close(con)
  gc()
  cat("\n")
  data;
}

#To avoid: argument 'object' must deparse to a single character string
fdata=textConnection( fetchLines("datafile.csv",'\\bP58\\b',fixed=F,maxlines = 100000))
df<-read.csv(fdata,header=T,sep=",",na.strings = c('NULL',''),fileEncoding = "UTF-8-BOM",stringsAsFactors = F)

R textConnection:“参数'对象'必须解析为单个字符串”

于 2016-04-06T04:43:54.947 回答
0

您现在可以在readr包中执行此操作。

library(tidyverse)
csvData <- read_csv(file="pf.csv") %>%
 filter(!ticker-column %in% c('ABT','ADCT')
于 2022-01-20T01:23:57.830 回答