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

Heinz Tuechler tuechler at gmx.at
Sat Dec 20 10:56:24 CET 2008


Dear Charles,

yes, your solution does what I need.
Maybe, it offers also a way to use the compare package with Surv objects.

Thank you,

Heinz


At 23:30 19.12.2008, Charles C. Berry wrote:
>On Fri, 19 Dec 2008, Heinz Tuechler wrote:
>
>>Dear David!
>>
>>Thank you for your response. I like csv files, 
>>because in that case I can easily compare 
>>different versions of similar data.frames. 
>>Similar in this case means that I may add a 
>>column or change some transformation command 
>>for one column. With dput it's rather 
>>difficult, and when I tried the compare 
>>package, I had no success comparing data.frames containing Surv objects.
>
>Heinz,
>
>Is this good enough?
>
>>mat <- as.data.frame( lapply( df.soac, unclass ) )
>>write.csv(mat,'mat.csv')
>>read.csv('mat.csv')
>   X soa.time soa.status char1
>1 1        1          0     1
>2 2        2          0     2
>3 3        3          1     3
>4 4        4          0     4
>5 5        5          1     5
>
>
>The bug seems to be in as.matrix.data.frame.
>
>HTH,
>
>Chuck
>
>>
>>Thanks again
>>
>>Heinz
>>
>>At 22:31 19.12.2008, David Winsemius wrote:
>>
>>>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.
>>
>>______________________________________________
>>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.
>
>Charles C. Berry                            (858) 534-2098
> 
>Dept of Family/Preventive Medicine
>E mailto:cberry at tajo.ucsd.edu               UC San Diego
>http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901



More information about the R-help mailing list