Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function write.csv returns an error

Tags:

r

csv

The R function write.csv returns an error.

Here's the data.frame I want to write:

> VaRStats
               A Index    B Index      C Index
Daily VaR     -0.65006    -0.50391     -0.5557
Daily CVaR    -0.75679    -0.57491    -0.65174
5 Days VaR     -1.4204     -1.0077     -1.2269

Here's the class of VaRStats:

> class(VaRStats)
[1] "data.frame"

And here's the dput() output of VaRStats:

> dput(VaRStats)
structure(list(`JWFXA Index` = structure(list(`NA` = -0.650061101980277, 
    `NA` = -0.756791819719978, `JWFXA Index` = -1.42035638029947), .Names = c(NA, 
NA, "JWFXA Index")), `CCYT1 Index` = structure(list(`NA` = -0.503912574910245, 
    `NA` = -0.574907003405759, `CCYT1 Index` = -1.00773735259718), .Names = c(NA, 
NA, "CCYT1 Index")), `FX Multistrategy Index` = structure(list(
    `NA` = -0.555699685451229, `NA` = -0.651738541799373, `FX Multistrategy Index` = -1.22688572580144), .Names = c(NA, 
NA, "FX Multistrategy Index"))), .Names = c("JWFXA Index", "CCYT1 Index", 
"FX Multistrategy Index"), row.names = c("Daily VaR", "Daily CVaR", 
"5 Days VaR"), class = "data.frame")

The error generated though the write.csv function

> write.csv(VaRStats, "SummaryStats.csv")
Error in write.table(x, file, nrow(x), p, rnames, sep, eol, na, dec, as.integer(quote),  : 
  type 'list' not implemented in 'EncodeElement'

How is that possible?

like image 492
Lorenzo Rigamonti Avatar asked Mar 15 '13 13:03

Lorenzo Rigamonti


3 Answers

Here you can simply do this :

      write.csv(as.matrix(dd), "SummaryStats.csv")

This works because all you columns are numeric.

read.csv("SummaryStats.csv")
         X JWFXA.Index CCYT1.Index FX.Multistrategy.Index
1  Daily VaR  -0.6500611  -0.5039126             -0.5556997
2 Daily CVaR  -0.7567918  -0.5749070             -0.6517385
3 5 Days VaR  -1.4203564  -1.0077374             -1.2268857

EDIT

This solution is the right one.

if you have a string in one of you list :

dd[1,1] <- 'a'

as.matrix(dd)
           JWFXA Index CCYT1 Index FX Multistrategy Index
Daily VaR  "a"         -0.5039126  -0.5556997            
Daily CVaR -0.7567918  -0.574907   -0.6517385            
5 Days VaR -1.420356   -1.007737   -1.226886 

but using the as.numeric will fail or at least convert to NA

sapply(dd, as.numeric)
     JWFXA Index CCYT1 Index FX Multistrategy Index
[1,]          NA  -0.5039126             -0.5556997
[2,]  -0.7567918  -0.5749070             -0.6517385
[3,]  -1.4203564  -1.0077374             -1.2268857
Warning message:
like image 152
agstudy Avatar answered Nov 20 '22 14:11

agstudy


For an unexplained reason, your data frame columns are lists, not vectors. You can transform your data frame back to a "normal" format with :

df <- sapply(VaRStats, as.numeric)
rownames(df) <- rownames(VarStats)
write.csv(df, "yourfile.csv")
like image 40
juba Avatar answered Nov 20 '22 13:11

juba


Your columns are lists. They should be vectors. Try this.

VaRStats<-sapply(VaRStats,unlist)
write.csv(VaRStats,file="...")
like image 1
ndoogan Avatar answered Nov 20 '22 13:11

ndoogan