计算一系列csv文件的行数 [英] Counting the number of rows of a series of csv files

查看:31
本文介绍了计算一系列csv文件的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 R 教程,并怀疑我必须使用其中一个函数,但我不确定是哪个(是的,我研究了它们,但直到我对 R 术语变得更加流利时,它们才变得非常混乱).

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

在我的工作目录中有一个文件夹specdata".Specdata 包含数百个名为 001.csv - 300.csv 的 CSV 文件.

In my working directory there is a folder "specdata". Specdata contains hundreds of CSV files named 001.csv - 300.csv.

我正在处理的函数必须计算输入的 csv 文件数量的总行数.因此,如果函数中的参数是 1:10 并且每个文件都有 10 行,则返回 100.

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.

这是我目前所拥有的:

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 作为参数,我会收到一个错误:

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

我应该怎么做才能从输入的 ID 参数中计算出总行数?

What should I do to be able to count the total number of rows from the inputed ID parameter?

推荐答案

read.csv 期望只读取一个文件,因此您需要遍历文件,R 惯用的方法是使用 sapply:

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)

例如,这是对您的 complete 函数的重写:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆