Peter Neumaier
2016-Mar-10 11:14 UTC
[R] Conversion problem with write.csv and as.character()
Hi all, sorry for double/cross posting, I have sent an initial, similar question accidentally to r-sig-finance. I am writing a matrix (typeof = double) into a CSV file with write.csv. My first column of the matrix is a date in the form yyyy-mm-dd hh:mm:ss:> a_fetchdata[1,0]2016-02-09 07:30:00> typeof(a_fetchdata[1,0])[1] "double" My CSV file contains a sequence of integers (from 1 to x) instead of the expected date. I tried to convert, but ran into "Error in dimnames":> as.character(first_fetchdata[1,0])Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) : 'dimnames' applied to non-array Called from: as.matrix.xts(x) Browse[1]> c>a) How can I prevent the conversion into integers to happen when writing into CSV? b) if a) is not do-able: how can I convert the date in double format to chars (i.e. with as.character) ? Thanks in advance, Peter [[alternative HTML version deleted]]
Joshua Ulrich
2016-Mar-10 15:24 UTC
[R] Conversion problem with write.csv and as.character()
I already answered all your questions on R-SIG-Finance. On Thu, Mar 10, 2016 at 5:14 AM, Peter Neumaier <peter.neumaier at gmail.com> wrote:> Hi all, sorry for double/cross posting, I have sent an initial, similar > question > accidentally to r-sig-finance. > > I am writing a matrix (typeof = double) into a CSV file with write.csv. > > My first column of the matrix is a date in the form yyyy-mm-dd hh:mm:ss: > >> a_fetchdata[1,0] > > 2016-02-09 07:30:00 >> typeof(a_fetchdata[1,0]) > [1] "double" > > My CSV file contains a sequence of integers (from 1 to x) instead of the > expected date. > > I tried to convert, but ran into "Error in dimnames": > >> as.character(first_fetchdata[1,0]) > Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) : > 'dimnames' applied to non-array > Called from: as.matrix.xts(x) > Browse[1]> c >> > > a) How can I prevent the conversion into integers to happen when writing > into CSV? > b) if a) is not do-able: how can I convert the date in double format to > chars (i.e. with as.character) ? > > Thanks in advance, > Peter > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2016 | www.rinfinance.com
Sarah Goslee
2016-Mar-10 15:25 UTC
[R] Conversion problem with write.csv and as.character()
Hi Peter, We really need a reproducible example to solve this kind of question. Please use dput(head(yourdata)) to provide a sample of data (or make up fake data that shows the same problems), and provide the code you're using. Sarah On Thu, Mar 10, 2016 at 6:14 AM, Peter Neumaier <peter.neumaier at gmail.com> wrote:> Hi all, sorry for double/cross posting, I have sent an initial, similar > question > accidentally to r-sig-finance. > > I am writing a matrix (typeof = double) into a CSV file with write.csv. > > My first column of the matrix is a date in the form yyyy-mm-dd hh:mm:ss: > >> a_fetchdata[1,0] > > 2016-02-09 07:30:00 >> typeof(a_fetchdata[1,0]) > [1] "double" > > My CSV file contains a sequence of integers (from 1 to x) instead of the > expected date. > > I tried to convert, but ran into "Error in dimnames": > >> as.character(first_fetchdata[1,0]) > Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) : > 'dimnames' applied to non-array > Called from: as.matrix.xts(x) > Browse[1]> c >> > > a) How can I prevent the conversion into integers to happen when writing > into CSV? > b) if a) is not do-able: how can I convert the date in double format to > chars (i.e. with as.character) ? > > Thanks in advance, > Peter >
Peter Neumaier
2016-Mar-10 20:37 UTC
[R] Conversion problem with write.csv and as.character()
All fixed and solved in R-SIG-Finance. Thanks, Peter On Thu, Mar 10, 2016 at 3:25 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:> Hi Peter, > > We really need a reproducible example to solve this kind of question. > Please use dput(head(yourdata)) to provide a sample of data (or make > up fake data that shows the same problems), and provide the code > you're using. > > Sarah > > On Thu, Mar 10, 2016 at 6:14 AM, Peter Neumaier > <peter.neumaier at gmail.com> wrote: > > Hi all, sorry for double/cross posting, I have sent an initial, similar > > question > > accidentally to r-sig-finance. > > > > I am writing a matrix (typeof = double) into a CSV file with write.csv. > > > > My first column of the matrix is a date in the form yyyy-mm-dd hh:mm:ss: > > > >> a_fetchdata[1,0] > > > > 2016-02-09 07:30:00 > >> typeof(a_fetchdata[1,0]) > > [1] "double" > > > > My CSV file contains a sequence of integers (from 1 to x) instead of the > > expected date. > > > > I tried to convert, but ran into "Error in dimnames": > > > >> as.character(first_fetchdata[1,0]) > > Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) : > > 'dimnames' applied to non-array > > Called from: as.matrix.xts(x) > > Browse[1]> c > >> > > > > a) How can I prevent the conversion into integers to happen when writing > > into CSV? > > b) if a) is not do-able: how can I convert the date in double format to > > chars (i.e. with as.character) ? > > > > Thanks in advance, > > Peter > > >[[alternative HTML version deleted]]
Hi Peter, Have you tried: a_fetchdata<-format(a_fetchdata,"%Y-%m-%d %H:%M:%S") before writing the data? Jim On Thu, Mar 10, 2016 at 10:14 PM, Peter Neumaier <peter.neumaier at gmail.com> wrote:> Hi all, sorry for double/cross posting, I have sent an initial, similar > question > accidentally to r-sig-finance. > > I am writing a matrix (typeof = double) into a CSV file with write.csv. > > My first column of the matrix is a date in the form yyyy-mm-dd hh:mm:ss: > >> a_fetchdata[1,0] > > 2016-02-09 07:30:00 >> typeof(a_fetchdata[1,0]) > [1] "double" > > My CSV file contains a sequence of integers (from 1 to x) instead of the > expected date. > > I tried to convert, but ran into "Error in dimnames": > >> as.character(first_fetchdata[1,0]) > Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) : > 'dimnames' applied to non-array > Called from: as.matrix.xts(x) > Browse[1]> c >> > > a) How can I prevent the conversion into integers to happen when writing > into CSV? > b) if a) is not do-able: how can I convert the date in double format to > chars (i.e. with as.character) ? > > Thanks in advance, > Peter > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.