计算一系列csv文件的行数 [英] Counting the number of rows of a series of csv files
问题描述
我正在研究R教程,并且怀疑我必须使用这些功能之一,但是我不确定是哪一个(是的,我已经研究了它们,但是直到我对R术语更加流利时,它们才变得令人困惑). /p>
在我的工作目录中有一个文件夹"specdata". Specdata包含数百个名为001.csv-300.csv的CSV文件.
我正在使用的函数必须计算输入的csv文件的总行数.因此,如果函数中的参数为1:10
并且每个文件都有十行,则返回100.
这是我到目前为止所拥有的:
complete <- function(directory,id = 1:332) {
setpath <- paste("/Users/gcameron/Desktop",directory,sep="/")
setwd(setpath)
csvfile <- sprintf("%03d.csv", id)
file <- read.csv(csvfile)
nrow(file)
}
当ID参数为一个数字(例如17)时,此方法有效.但是,如果我输入10:50作为参数,则会收到错误消息:
Error in file(file, "rt") : invalid 'description' argument
我该怎么做才能从输入的ID参数计算总行数?
read.csv
期望只读取一个文件,因此您需要循环遍历文件,一种惯用的方法是使用sapply
:
nrows <- sapply( csvfile, function(f) nrow(read.csv(f)) )
sum(nrows)
例如,这是您的complete
函数的重写:
complete <- function(directory,id = 1:332) {
csvfiles <- sprintf("/Users/gcameron/Desktop/%s/%03d.csv", directory, id)
nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)) )
sum(nrows)
}
I'm working through an R tutorial and suspect that I have to use one of these functions but I'm not sure which (Yes I researched them but until I become more fluent in R terminology they are quite confusing).
In my working directory there is a folder "specdata". Specdata contains hundreds of CSV files named 001.csv - 300.csv.
The function I am working on must count the total number of rows for an inputed number of csv files. So if the argument in the function is 1:10
and each of those files has ten rows, return 100.
Here's what I have so far:
complete <- function(directory,id = 1:332) {
setpath <- paste("/Users/gcameron/Desktop",directory,sep="/")
setwd(setpath)
csvfile <- sprintf("%03d.csv", id)
file <- read.csv(csvfile)
nrow(file)
}
This works when the ID argument is one number, say 17. But, if I input say 10:50 as an argument, I receive an error:
Error in file(file, "rt") : invalid 'description' argument
What should I do to be able to count the total number of rows from the inputed ID parameter?
read.csv
expects to read just one file, so you need to loop over files, a R idiomatic way of doing so is to use sapply
:
nrows <- sapply( csvfile, function(f) nrow(read.csv(f)) )
sum(nrows)
For example, here is a rewrite of your complete
function:
complete <- function(directory,id = 1:332) {
csvfiles <- sprintf("/Users/gcameron/Desktop/%s/%03d.csv", directory, id)
nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)) )
sum(nrows)
}
这篇关于计算一系列csv文件的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!