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?
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:
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")
Your columns are lists. They should be vectors. Try this.
VaRStats<-sapply(VaRStats,unlist)
write.csv(VaRStats,file="...")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With