简体   繁体   English

从R中的多个csv文件读取时在数据框中追加一行

[英]Appending a row to a dataframe while reading from multiple csv files in R

I'm reading from multiple csv files in a loop, and performing some calculations on each file's data, and then I wish to add that new row to a data frame: 我正在循环读取多个csv文件,并对每个文件的数据进行一些计算,然后希望将该新行添加到数据框中:

for (i in csvFiles) {
    fileToBeRead<-paste(directory, i, sep="/")

    dataframe<-read.csv(paste(fileToBeRead, "csv", sep="."))
    file <- i
    recordsOK <- sum(complete.cases(dataframe))

    record.data <- data.frame(monitorID, recordsOK)
} 

So, I want to add file and recordsOK as a new row to the data frame. 因此,我想将file和recordsOK作为新行添加到数据框。 This just overwrites data frame every time, so I'd end up with the data from the latest csv file. 每次都只会覆盖数据帧,因此我将获得最新的csv文件中的数据。 How can I do this while preserving the data from the last iteration? 在保留上一次迭代的数据的同时,我该如何做?

Building a data.frame one row at a time is almost always the wrong way to do it. 一次建立一个data.frame几乎总是错误的方式。 Here'a more R-like solution 这是一个更像R的解决方案

OKcount<-sapply(csvFiles, function(i) {
    fileToBeRead<-paste(directory, i, sep="/")

    dataframe<-read.csv(paste(fileToBeRead, "csv", sep="."))
    sum(complete.cases(dataframe))
})

record.data <- data.frame(monitorID=seq_along(csvFiles), recordsOK=OKcount)

The main idea is that you generally build your data column-wise, not row-wise, and then bundle it together in a data.frame when you're all done. 主要思想是通常按列而不是按行构建数据,然后在完成后将其捆绑到data.frame中。 Because R has so many vectorized operations, this is usually pretty easy. 由于R具有许多矢量化运算,因此这通常很容易。

But if you really want to add rows to a data.frame, you can rbind (row bind) additional rows in. So instead of overwriting record.data each time, you would do 但是,如果你真的想行添加到data.frame,你可以rbind在(行绑定)附加行。所以不是覆盖的record.data每一次,你会怎么做

record.data <- rbind(record.data, data.frame(monitorID, recordsOK)

But that means you will need to define record.data outside of your loop and initialize it with the correct column names and data types since only matching data.frames can be combined. 但这意味着您将需要在record.data之外定义record.data并使用正确的列名和数据类型对其进行初始化,因为只能组合匹配的data.frames。 You can initialize it with 您可以使用

record.data <- data.frame(monitorID=numeric(), recordsOK=numeric())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM