Adam Amin
Adam Amin

Reputation: 1456

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?

Upvotes: 4

Views: 4718

Answers (3)

Abednego Nasila
Abednego Nasila

Reputation: 165

Use the function write.table() and save your file as .txt. R will append both the variables names and their values. You can remove the variables names from values using transformation functions such as anti_join() and filter() from dplyr.

Upvotes: 1

Aur&#232;le
Aur&#232;le

Reputation: 12819

An idiomatic way in R would be to rbind your data frames and write once:

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

Upvotes: 2

James
James

Reputation: 66844

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.

Upvotes: 9

Related Questions