我有以下R脚本:
target <- c("Autocorrelation","InformationContent_zero","InformationContent_one","InformationContent_two",
"InformationContent_four","PartialIC_zero","PartialIC_one","PartialIC_two","PartialIC_four",
"DBI_zero","DBI_one","DBI_two","DBI_four")
for (tar in target){
AC_data <- subset(data, select=c(tar))
AC <- aggregate(AC_data , list(class=data$TARGET_CLASS), mean)
ordered_AC <- AC[order(-AC[tar]),]
write.csv(ordered_AC,file="/home/nasser/Desktop/Experiments/evosuite-report/finalData.csv",append=TRUE)
}
当我使用我处理的数据运行脚本时,我得到以下结果:
class Autocorrelation
16 SomeExternalClass11 0.26582445
3 MoreMethodsModified 0.21295785
10 MultiPlateauxModified 0.19942221
1 DoublePeaks 0.19534564
class InformationContent_zero
2 MoreMethods 0.17936452
7 MultiPeaks 0.13527150
12 NeedleInHaystackNoParm 0.11714634
6 MultiMethodsModified 0.07180512
class InformationContent_one
2 MoreMethods 0.17936452
7 MultiPeaks 0.13527150
12 NeedleInHaystackNoParm 0.11714634
6 MultiMethodsModified 0.07180512
问题是写入CSV文件的唯一数据是最后一组class InformationContent_one
这意味着前两组被覆盖。您知道如何解决此问题并将所有数据写入CSV文件吗?
答案 0 :(得分:3)
在write.table
的帮助中,它声明:
write.csv和write.csv2为写入CSV提供了便利包装 文件。
...
这些包装是故意不灵活的:它们是 旨在确保使用正确的约定来编写 有效档案。尝试更改append,col.names,sep,dec或qmethod 被忽略,并发出警告。
如果要追加,则需要明确使用write.table
。
答案 1 :(得分:1)
R中的惯用方法是rbind
您的数据框并写一次:
write.csv(
do.call(rbind, lapply(target, function(tar) {
AC_data <- subset(data, select = c(tar))
AC <- aggregate(AC_data , list(class = data$TARGET_CLASS), mean)
res <- AC[order(-AC[tar]),]
res$var <- names(res)[2]
names(res)[2] <- "value"
res
})),
file = "/home/nasser/Desktop/Experiments/evosuite-report/finalData.csv"
)