Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 导入数据帧时过滤多个csv文件_R_Csv_Import_Filter - Fatal编程技术网

R 导入数据帧时过滤多个csv文件

R 导入数据帧时过滤多个csv文件,r,csv,import,filter,R,Csv,Import,Filter,我有大量的csv文件要读入R。csv中的所有列标题都是相同的。但我只想将每个文件中的行导入到数据帧中,其中变量在给定范围内(高于最小阈值&低于最大阈值),例如 过滤v3(v3>2&v3 minMZ&tempData[,1]

我有大量的csv文件要读入R。csv中的所有列标题都是相同的。但我只想将每个文件中的行导入到数据帧中,其中变量在给定范围内(高于最小阈值&低于最大阈值),例如

过滤v3(v3>2&v3 minMZ&tempData[,1]
谢谢你的建议

这里是一种使用
数据表的方法,它允许您使用
fread
(即)和
rbindlist
,这两种方法非常适合这种情况。它还有一个介于

library(data.table)
fileNames <- list.files(path = workDir)
alldata <- rbindlist(lapply(fileNames, function(x,mon,max) {
  xx <- fread(x, sep = ',')
  xx[, fileID :=   gsub(".csv.*", "", x)]
  xx[between(v3, lower=min, upper = max, incbounds = FALSE)]
  }, min = 2, max = 3))
库(data.table)

文件名如果您想在导入数据之前进行“筛选”,请尝试使用
read.csv.sql
from

如果您确实内存不足,则以下解决方案可能会起作用。它使用
LaF
仅读取过滤所需的列;然后计算将被读取的总行数;初始化完整的data.frame,然后从文件中读取所需的行。(它可能不比其他解决方案快)

库(“LaF”)

colnames导入时为什么要筛选?您当前的解决方案是否存在速度或内存问题?如果是速度,那么问题可能是rbind。如果它是内存,那么它可能仍然是rbind,但是首先过滤可能会有所帮助。@Jan van der Laan-是的,问题是内存不足……这就是为什么过滤掉不重要的内容应该是一种方法。我建议您查看
sqldf
包,它应该会对您有所帮助。谢谢您的建议,我试过了,速度确实更快。最后,我得到了一个不同的解决方案,请参见上文。@mnel:small-typo:看起来你的“mon”应该是第三行的“min”。
   v1   v2   v3
1  c    w    4
2  v    e    5
#Read the data files
fileNames <- list.files(path = workDir)
mergedFiles <- do.call("rbind", sapply(fileNames, read.csv, simplify = FALSE))
fileID <- row.names(mergedFiles)
fileID <- gsub(".csv.*", "", fileID)
#Combining data with file IDs
combFiles=cbind(fileID, mergedFiles)
#Filtering the data according to criteria
resultFile <- combFiles[combFiles$v3 > min & combFiles$v3 < max, ]
Edit
fileNames = list.files(path = workDir)
mzList = list()
for(i in 1:length(fileNames)){
tempData = read.csv(fileNames[i])
mz.idx = which(tempData[ ,1] > minMZ & tempData[ ,1] < maxMZ)
mz1 = tempData[mz.idx, ]
mzList[[i]] = data.frame(mz1, filename = rep(fileNames[i], length(mz.idx)))
}
resultFile = do.call("rbind", mzList)
library(data.table)
fileNames <- list.files(path = workDir)
alldata <- rbindlist(lapply(fileNames, function(x,mon,max) {
  xx <- fread(x, sep = ',')
  xx[, fileID :=   gsub(".csv.*", "", x)]
  xx[between(v3, lower=min, upper = max, incbounds = FALSE)]
  }, min = 2, max = 3))
library("LaF")

colnames <- c("v1","v2","v3")
colclasses <- c("character", "character", "numeric")

fileNames <- list.files(pattern = "*.csv")

# First determine which lines to read from each file and the total number of lines
# to be read
lines <- list()
for (fn in fileNames) {
  laf <- laf_open_csv(fn, column_types=colclasses, column_names=colnames, skip=1)
  d   <- laf$v3[] 
  lines[[fn]] <- which(d > 2 & d < 7)
}
nlines <- sum(sapply(lines, length))

# Initialize data.frame
df <- as.data.frame(lapply(colclasses, do.call, list(nlines)), 
        stringsAsFactors=FALSE)
names(df) <- colnames

# Read the lines from the files
i <- 0
for (fn in names(lines)) {
  laf <- laf_open_csv(fn, column_types=colclasses, column_names=colnames, skip=1)
  n   <- length(lines[[fn]])
  df[seq_len(n) + i, ] <- laf[lines[[fn]], ]
  i   <- i + n
}