Mustard Tiger
Mustard Tiger

Reputation: 3671

R write list to CSV line by line

I have the following code in R:

library(party)
dat = read.csv("data.csv", header = TRUE)

train <- dat[1:1000, ]
test <- dat[1000:1200, ]

output.tree <- cforest(t_class ~ var1 + var2,
                       data = train)

train_predict <- predict(output.tree, newdata = test, OOB=TRUE, type = "prob")

for (name in names(train_predict))
{
  p <- (train_predict[[name]][1:3])
  write.table(p, file = "result.csv",col.names = FALSE, append=TRUE)
}

I am trying to write the result of the random forest prediction to a csv file.

The result train_predict looks like the following:

enter image description here

When I run the above code its only write the first column of each row to the csv and not all three.

How can I write all three columns of the list to the file?

Also is there a way in R to clear the csv before you write to it in case there is something in it already?

Upvotes: 2

Views: 6424

Answers (2)

arvi1000
arvi1000

Reputation: 9582

Rather than write serially, you can convert to a data.frame and just write all at once:

Generate fake data that looks similar to what you posted:

fakeVec <- function(dummy) t(setNames(rnorm(3), letters[1:3]))
my_list <- lapply(0:4, fakeVec)
names(my_list) <- 6000:6004

Here's the fake data:

$`6000`
              a          b         c
[1,] -0.2444195 -0.2189598 -1.442364

$`6001`
             a        b          c
[1,] 0.2742636 1.068294 -0.8335477

$`6002`
            a        b         c
[1,] -1.13298 1.927268 -2.123603

$`6003`
             a        b            c
[1,] 0.8260184 1.003259 -0.003590849

$`6004`
              a         b         c
[1,] -0.2025963 0.1192242 -1.121807

Then convert format:

# crush to flat matrix
my_mat <- do.call(rbind, my_list)
# add in list names as new column
my_df <- data.frame(id = names(my_list), my_mat)

Now you have a data.frame like this:

    id          a          b            c
1 6000 -0.2444195 -0.2189598 -1.442364429
2 6001  0.2742636  1.0682937 -0.833547659
3 6002 -1.1329796  1.9272681 -2.123603334
4 6003  0.8260184  1.0032591 -0.003590849
5 6004 -0.2025963  0.1192242 -1.121807439

Which you can just write straight to a file:

write.csv(my_df, 'my_file.csv', row.names=F)

Upvotes: 1

jcp
jcp

Reputation: 851

How about this?

temp = list(x = data.frame(a = "a", b = "b", c = "c"),
        y = data.frame(d = "d", e = "e", f = "f"),
        z = data.frame(g = "g", h = "h", i = "i"))

for (i in 1:length(temp)) {
  write.table(temp[[i]], "temp.csv",col.names = F, append = T)
}

Regarding clearing the csv. If I understood your question correctly, just erase the append = T?

Upvotes: 0

Related Questions