Write.csv function returns an error

The R function write.csv returns an error.

Here 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 VaRStats class:

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

And here is the output of dput() 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") 

Error created using 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 this possible?

+4
source share
4 answers

Here you can simply do this:

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

This works because all 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 decision is correct.

if you have a line in one of the lists:

 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 as.numeric will crash, 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: 
+6
source

For inexplicable reasons, your data frame columns are lists, not vectors. You can convert your data frame to the β€œnormal” format with:

 df <- sapply(VaRStats, as.numeric) rownames(df) <- rownames(VarStats) write.csv(df, "yourfile.csv") 
+4
source

Your columns are lists. They must be vectors. Try it.

 VaRStats<-sapply(VaRStats,unlist) write.csv(VaRStats,file="...") 
+1
source

Can you give a reproducible example, I assume you have a problem with row.names and col.names , also use write.table so that you can set all the arguments as you want

EDIT: I have the same problem on str !!!

 R) str(test) 'data.frame': 3 obs. of 3 variables: $ JWFXA Index :List of 3 ..$ NA : num -0.65 ..$ NA : num -0.757 ..$ JWFXA Index: num -1.42 $ CCYT1 Index :List of 3 ..$ NA : num -0.504 ..$ NA : num -0.575 ..$ CCYT1 Index: num -1.01 $ FX Multistrategy Index:List of 3 ..$ NA : num -0.556 ..$ NA : num -0.652 ..$ FX Multistrategy Index: num -1.23 

your data is not shared data.frame

@juba has a nice fix, use lapply to cast each list in numeric vector

0
source

Source: https://habr.com/ru/post/1469523/


All Articles