简体   繁体   English

在CSV文件中将矢量作为行附加

[英]Append a vector as a row in a CSV file

What I already have: 我已经拥有的东西:

Consider the following data — just dummy data, the real data is created automatically: 考虑以下数据 - 只是虚拟数据,自动创建实际数据:

features <- numeric(0)
features <- c(features, 1, 2, 3, 4, 5, 6)
features2 <- numeric(0)
features2 <- c(features2, 1, 2, 3, 4, 5, 6)
featureVectors <- list()
featureVectors[["file1"]] <- features
featureVectors[["file2"]] <- features2
resultMatrix <- do.call(rbind, featureVectors)
colnames(resultMatrix) <- c("SPEC_MEAN", "SPEC_SD", "SPEC_MODE", "AUTOC_MEAN", "AUTOC_SD", "ZC_MEAN")

This produces an output like the following: 这会产生如下输出:

      SPEC_MEAN SPEC_SD SPEC_MODE AUTOC_MEAN AUTOC_SD ZC_MEAN
file1         1       2         3          4        5       6
file2         1       2         3          4        5       6

I can write this to a CSV file by calling write.csv(resultMatrix, file="out.csv") . 我可以通过调用write.csv(resultMatrix, file="out.csv")将其写入CSV文件。


What I need: 我需要的:

As the result file is (currently) just created on the fly, I'd like to write these features (ie the vectors) to the CSV file as soon they are evaluated. 由于结果文件(当前)刚刚创建,我想在评估后立即将这些features (即向量)写入CSV文件。

So I thought I'd use write.csv and append , but the option isn't available for the method. 所以我以为我会使用write.csvappend ,但该选项不适用于该方法。 I then thought I could use write.table , but there are two problems: 然后我想我可以使用write.table ,但有两个问题:

  1. write.table does not indent the first empty column name, thus leaving my first row shifted one to the left. write.table不会缩进第一个空列名,从而使我的第一行向左移动一行。

  2. The data is somehow incorrectly transposed. 数据以某种方式错误地转置。 See example below. 见下面的例子。


What I've tried: 我尝试过的:

Also, calling these commands … 另外,调用这些命令......

write.table(resultMatrix, file="data-appended.csv", sep=",")
write.table(features, file="data-appended.csv", sep=",", append=TRUE, col.names=FALSE)

… produces this result: ...产生这个结果:

"SPEC_MEAN","SPEC_SD","SPEC_MODE","AUTOC_MEAN","AUTOC_SD","ZC_MEAN"
"file1",1,2,3,4,5,6
"file2",1,2,3,4,5,6
"1",1
"2",2
"3",3
"4",4
"5",5
"6",6

… which is not what I want. ......这不是我想要的。 So, how do I append the content of the features vector, including the file name, to an already existing CSV file? 那么,如何将features向量的内容(包括file名)附加到现有的CSV文件中?

Two adjustments will solve your two problems. 两次调整将解决您的两个问题。

  • To keep indentation of the column headers, use the unintuitive (but documented!) argument col.names=NA : 要保持列标题的缩进,请使用不直观(但记录在案)!参数col.names=NA

     write.table(resultMatrix, file = "data-appended.csv", sep = ",", col.names = NA) 
  • To write features (a vector) as a row rather than a column, transpose it and convert it to a matrix before passing it to write.table() : features (载体)作为行,而不是一列,转置,并将其传递到前转换为矩阵write.table()

     FF <- as.matrix(t(features)) write.table(FF, file = "data-appended.csv", sep = ",", col.names = FALSE, append=TRUE) 

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

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