Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

Append doesn't work when writing to CSV in R

I have the following R script:

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

When I run the script with the data that I process I get the following results:

                    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

The problem is that the only data that is written to the CSV file is the last group which is class InformationContent_one

That means the first two groups are overwritten. Do you know how to fix this and let all the data are written to the CSV file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the help for write.table, it states:

write.csv and write.csv2 provide convenience wrappers for writing CSV files.

...

These wrappers are deliberately inflexible: they are designed to ensure that the correct conventions are used to write a valid file. Attempts to change append, col.names, sep, dec or qmethod are ignored, with a warning.

If you want to append, you need to use write.table explicitly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...