[R] How to write a Surv object to a csv-file?

David Winsemius dwinsemius at comcast.net
Fri Dec 19 22:31:13 CET 2008


On Dec 19, 2008, at 2:04 PM, Heinz Tuechler wrote:

> Dear All,
>
> trying to write a data.frame, containing Surv objects to a csv-file  
> I get
> "Error in dimnames(X) <- list(dn[[1L]], unlist(collabs, use.names =  
> FALSE)) :
>  length of 'dimnames' [2] not equal to array extent".
>
> See example below.
>
> May be, I overlooked something, but I expected that also data.frames  
> containing Surv objects may be written to csv files.
>
> Is there a better way to write to csv files?

Yes, if the goal is creating an ASCII structure that can be recovered  
by an R interpreter:

?dput
?dget

 > dput(df.soac, "test")
 > copy.df.soac <- dget("test")
 > all.equal(df.soac, copy.df.soac)

  Doesn't give you a result that you would want to read with Excel,  
but that does not appear to be your goal. You can examine it with a  
text editor.

-- 
David Winsemius


>
>
> Thanks,
>
> Heinz Tüchler
>
>
>
> ###   write Surv-object in csv-file
> library(survival)
> ## create example data
> soa <- Surv(1:5, c(0, 0, 1, 0, 1))
> df.soa <- data.frame(soa)
> write.csv(df.soa, 'df.soa.csv')    ## works as I expected
> read.csv('df.soa.csv')	            ## works as I expected
>
> df.soa2 <- data.frame(soa, soa2=soa)
> write.csv(df.soa2, 'df.soa2.csv')  ## works as I expected
> read.csv('df.soa2.csv')            ## works as I expected
>
> char1 <- letters[1:5]
> df.soac <- data.frame(soa, char1)
> write.csv(df.soac, 'df.soac.csv')  ## generates the following error  
> message:
>
> Error in dimnames(X) <- list(dn[[1L]], unlist(collabs, use.names =  
> FALSE)) :
>  length of 'dimnames' [2] not equal to array extent
>
> df.csoa <- data.frame(char1, soa)
> write.csv(df.csoa, 'df.soac.csv')  ## generates the following error  
> message:
>
> Error in dimnames(X) <- list(dn[[1L]], unlist(collabs, use.names =  
> FALSE)) :
>  length of 'dimnames' [2] not equal to array extent
>
>
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status         Patched
> major          2
> minor          8.0
> year           2008
> month          11
> day            10
> svn rev        46884
> language       R
> version.string R version 2.8.0 Patched (2008-11-10 r46884)
> > sessionInfo()
> R version 2.8.0 Patched (2008-11-10 r46884)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=German_Austria.1252;LC_CTYPE=German_Austria. 
> 1252;LC_MONETARY=German_Austria. 
> 1252;LC_NUMERIC=C;LC_TIME=German_Austria.1252
>
> attached base packages:
> [1] splines   stats     graphics  grDevices utils     datasets   
> methods
> [8] base
>
> other attached packages:
> [1] survival_2.34-1
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list