Dear R-user, I have read a dataset from .csv file into R. This dataset includes one column containing some data in 'date and time' format, e.g. 'dd/mm/yyyy hh:mm'. These data were automatically read and saved as 'factor' in R. When I was trying to produce some plots (such as time series) with the above 'date and time' on x-axis, it caused some disodering problem, e.g. 1st of March 2012 is in front of 10th of Feb. 2012 (if the data is from 10th Feb. 2012 to 1st of March 2012). I understand that I might have to convert them from 'factor' to 'date' first, so I tried using 'as.date'. But this method seems only work for data in format of 'd/m/y' and no further option that allows me to add hours and minutes. I checked online for other methods such as 'as.POSIX' and 'strptime' but none of them seem to offer me a quick solution. Please note that the data I received is recorded every 10 minutes so they are saved in the form of 'dd/mm/yyyy hh:mm', e.g. I only have data measured up to 'minute' NOT to 'second'. Are there any direct solution that I can solve this issue?? Many thanks in advance! HJ [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Mar-13 16:30 UTC
[R] Converting factor data into Date-time format
as.POSIXct(as.character(FACTORHERE), format = "%d/%m/%Y %H:%M") Michael On Tue, Mar 13, 2012 at 12:20 PM, Haojie Yan <yhj204 at googlemail.com> wrote:> Dear R-user, > > I have read a dataset from .csv file into R. This dataset includes one > column containing some data in 'date and time' format, e.g. 'dd/mm/yyyy > hh:mm'. > > These data were automatically read and saved as 'factor' in R. When I was > trying to produce some plots (such as time series) with the above 'date and > time' on x-axis, ?it caused some disodering problem, e.g. 1st of March 2012 > is in front of 10th of Feb. 2012 (if the data is from 10th Feb. 2012 to 1st > of March 2012). I understand that I might have to convert them from > 'factor' to 'date' first, so I tried using 'as.date'. But this method seems > only work for data in format of ?'d/m/y' and no further option that allows > me to add hours and minutes. > > I checked online for other methods such as 'as.POSIX' and 'strptime' but > none of them seem to offer me a quick solution. > > Please note that the data I received is recorded every 10 minutes so they > are saved in the form of ?'dd/mm/yyyy hh:mm', e.g. I only have data > measured up to 'minute' NOT to ?'second'. Are there any direct solution > that I can solve this issue?? > > > Many thanks in advance! > HJ > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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.
On Tue, Mar 13, 2012 at 12:20 PM, Haojie Yan <yhj204 at googlemail.com> wrote:> Dear R-user, > > I have read a dataset from .csv file into R. This dataset includes one > column containing some data in 'date and time' format, e.g. 'dd/mm/yyyy > hh:mm'. > > These data were automatically read and saved as 'factor' in R. When I was > trying to produce some plots (such as time series) with the above 'date and > time' on x-axis, ?it caused some disodering problem, e.g. 1st of March 2012 > is in front of 10th of Feb. 2012 (if the data is from 10th Feb. 2012 to 1st > of March 2012). I understand that I might have to convert them from > 'factor' to 'date' first, so I tried using 'as.date'. But this method seems > only work for data in format of ?'d/m/y' and no further option that allows > me to add hours and minutes. > > I checked online for other methods such as 'as.POSIX' and 'strptime' but > none of them seem to offer me a quick solution. > > Please note that the data I received is recorded every 10 minutes so they > are saved in the form of ?'dd/mm/yyyy hh:mm', e.g. I only have data > measured up to 'minute' NOT to ?'second'. Are there any direct solution > that I can solve this issue??See Example 6 in the document Reading Data in zoo: http://cran.r-project.org/package=zoo -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
R. Michael Weylandt
2012-Mar-13 17:09 UTC
[R] Converting factor data into Date-time format
Just a little typo: see below. On Tue, Mar 13, 2012 at 1:00 PM, Haojie Yan <yhj204 at googlemail.com> wrote:> Dear Michael, > > Thanks a lot for your hints. > > I have just had a try as below but still got back some error messages as > shown: > > The?object containing the 'date_time' data is?named 'INTERVAL_END_TIME'?and > and wanted to plot it against another?variable 'CHANNEL_01' (data type: > numerical). > > > As you suggested I did the following... > > (1) Converting 'INTERVAL_END_TIME' into charactor first and name it > 'DATE_TIME_1'; > (2) Converting ?'DATE_TIME_1' into "POSIXlt" "POSIXt" type data > > But, seems it doesnt work well and I got 'NA's for all of them... > > Any thoughts?? > > Many thanks again! > HJ > > >> INTERVAL_END_TIME[1:10] > ?[1] 20/02/2012 00:10 20/02/2012 00:20 20/02/2012 00:30 20/02/2012 00:40 > 20/02/2012 00:50 > ?[6] 20/02/2012 01:00 20/02/2012 01:10 20/02/2012 01:20 20/02/2012 01:30 > 20/02/2012 01:40 > 1584 Levels: 01/03/2012 00:00 01/03/2012 00:10 01/03/2012 00:20 ... > 29/02/2012 23:50 > >> class(INTERVAL_END_TIME) > [1] "factor" > >> DATE_TIME_1<-as.character(INTERVAL_END_TIME) > >> DATE_TIME_1[1:10] > ?[1] "20/02/2012 00:10" "20/02/2012 00:20" "20/02/2012 00:30" "20/02/2012 > 00:40" > ?[5] "20/02/2012 00:50" "20/02/2012 01:00" "20/02/2012 01:10" "20/02/2012 > 01:20" > ?[9] "20/02/2012 01:30" "20/02/2012 01:40" > >> class(DATE_TIME_1) > [1] "character" > >> DATE_TIME_2<-as.POSIXlt(DATE_TIME_1,format="%d/%m/%y %H:%M")This needs to be a capital "Y" as in my original post. e.g., x <- c("20/02/2012 00:10", "20/02/2012 00:20", "20/02/2012 00:30", "20/02/2012 00:40") as.POSIXct(x, format = "%d/%m/%y %H:%M") # No good :-( as.POSIXct(x, format = "%d/%m/%Y %H:%M") # Good! Michael> >> DATE_TIME_2[1:10] > ?[1] NA NA NA NA NA NA NA NA NA NA > >> class(DATE_TIME_2) > [1] "POSIXlt" "POSIXt" > >> plot(DATE_TIME_2,CHANNEL_01) > Error in plot.window(...) : need finite 'xlim' values > In addition: Warning messages: > 1: In min(x) : no non-missing arguments to min; returning Inf > 2: In max(x) : no non-missing arguments to max; returning -Inf >> > > On Tue, Mar 13, 2012 at 4:30 PM, R. Michael Weylandt > <michael.weylandt at gmail.com> wrote: >> >> as.POSIXct(as.character(FACTORHERE), format = "%d/%m/%Y %H:%M") >> >> Michael >> >> On Tue, Mar 13, 2012 at 12:20 PM, Haojie Yan <yhj204 at googlemail.com> >> wrote: >> > Dear R-user, >> > >> > I have read a dataset from .csv file into R. This dataset includes one >> > column containing some data in 'date and time' format, e.g. 'dd/mm/yyyy >> > hh:mm'. >> > >> > These data were automatically read and saved as 'factor' in R. When I >> > was >> > trying to produce some plots (such as time series) with the above 'date >> > and >> > time' on x-axis, ?it caused some disodering problem, e.g. 1st of March >> > 2012 >> > is in front of 10th of Feb. 2012 (if the data is from 10th Feb. 2012 to >> > 1st >> > of March 2012). I understand that I might have to convert them from >> > 'factor' to 'date' first, so I tried using 'as.date'. But this method >> > seems >> > only work for data in format of ?'d/m/y' and no further option that >> > allows >> > me to add hours and minutes. >> > >> > I checked online for other methods such as 'as.POSIX' and 'strptime' but >> > none of them seem to offer me a quick solution. >> > >> > Please note that the data I received is recorded every 10 minutes so >> > they >> > are saved in the form of ?'dd/mm/yyyy hh:mm', e.g. I only have data >> > measured up to 'minute' NOT to ?'second'. Are there any direct solution >> > that I can solve this issue?? >> > >> > >> > Many thanks in advance! >> > HJ >> > >> > ? ? ? ?[[alternative HTML version deleted]] >> > >> >> > ______________________________________________ >> > 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. Michael Weylandt
2012-Mar-13 18:10 UTC
[R] Converting factor data into Date-time format
No problem. A pro-tip for future posts: the dput() function creates a plain text representation of the data in question which is great for email and is nicely copy-and-pasteable. It wasn't so much a thing here, but for large or complicated data sets, the regular console printout doesn't always reveal all the details in play. (This can be particularly nasty when dealing with time objects) Best, Michael Weylandt On Tue, Mar 13, 2012 at 1:28 PM, Haojie Yan <yhj204 at googlemail.com> wrote:> Hi Michael!!! > > As a first time R-help user I just wanted to say THANKS A MILLION!!!!!!! for > your prompt and very helpful reply!!! > > Still can not believe this annoying issue can be resolved that quickly!!! > > Brilliant-will visit this forum soon!! > HJ > > On Tue, Mar 13, 2012 at 5:09 PM, R. Michael Weylandt > <michael.weylandt at gmail.com> wrote: >> >> Just a little typo: see below. >> >> On Tue, Mar 13, 2012 at 1:00 PM, Haojie Yan <yhj204 at googlemail.com> wrote: >> > Dear Michael, >> > >> > Thanks a lot for your hints. >> > >> > I have just had a try as below but still got back some error messages as >> > shown: >> > >> > The?object containing the 'date_time' data is?named >> > 'INTERVAL_END_TIME'?and >> > and wanted to plot it against another?variable 'CHANNEL_01' (data type: >> > numerical). >> > >> > >> > As you suggested I did the following... >> > >> > (1) Converting 'INTERVAL_END_TIME' into charactor first and name it >> > 'DATE_TIME_1'; >> > (2) Converting ?'DATE_TIME_1' into "POSIXlt" "POSIXt" type data >> > >> > But, seems it doesnt work well and I got 'NA's for all of them... >> > >> > Any thoughts?? >> > >> > Many thanks again! >> > HJ >> > >> > >> >> INTERVAL_END_TIME[1:10] >> > ?[1] 20/02/2012 00:10 20/02/2012 00:20 20/02/2012 00:30 20/02/2012 00:40 >> > 20/02/2012 00:50 >> > ?[6] 20/02/2012 01:00 20/02/2012 01:10 20/02/2012 01:20 20/02/2012 01:30 >> > 20/02/2012 01:40 >> > 1584 Levels: 01/03/2012 00:00 01/03/2012 00:10 01/03/2012 00:20 ... >> > 29/02/2012 23:50 >> > >> >> class(INTERVAL_END_TIME) >> > [1] "factor" >> > >> >> DATE_TIME_1<-as.character(INTERVAL_END_TIME) >> > >> >> DATE_TIME_1[1:10] >> > ?[1] "20/02/2012 00:10" "20/02/2012 00:20" "20/02/2012 00:30" >> > "20/02/2012 >> > 00:40" >> > ?[5] "20/02/2012 00:50" "20/02/2012 01:00" "20/02/2012 01:10" >> > "20/02/2012 >> > 01:20" >> > ?[9] "20/02/2012 01:30" "20/02/2012 01:40" >> > >> >> class(DATE_TIME_1) >> > [1] "character" >> > >> >> DATE_TIME_2<-as.POSIXlt(DATE_TIME_1,format="%d/%m/%y %H:%M") >> >> This needs to be a capital "Y" as in my original post. >> >> e.g., >> >> x <- c("20/02/2012 00:10", "20/02/2012 00:20", "20/02/2012 00:30", >> "20/02/2012 00:40") >> >> as.POSIXct(x, format = "%d/%m/%y %H:%M") # No good :-( >> as.POSIXct(x, format = "%d/%m/%Y %H:%M") # Good! >> >> >> >> Michael >> >> > >> >> DATE_TIME_2[1:10] >> > ?[1] NA NA NA NA NA NA NA NA NA NA >> > >> >> class(DATE_TIME_2) >> > [1] "POSIXlt" "POSIXt" >> > >> >> plot(DATE_TIME_2,CHANNEL_01) >> > Error in plot.window(...) : need finite 'xlim' values >> > In addition: Warning messages: >> > 1: In min(x) : no non-missing arguments to min; returning Inf >> > 2: In max(x) : no non-missing arguments to max; returning -Inf >> >> >> > >> > On Tue, Mar 13, 2012 at 4:30 PM, R. Michael Weylandt >> > <michael.weylandt at gmail.com> wrote: >> >> >> >> as.POSIXct(as.character(FACTORHERE), format = "%d/%m/%Y %H:%M") >> >> >> >> Michael >> >> >> >> On Tue, Mar 13, 2012 at 12:20 PM, Haojie Yan <yhj204 at googlemail.com> >> >> wrote: >> >> > Dear R-user, >> >> > >> >> > I have read a dataset from .csv file into R. This dataset includes >> >> > one >> >> > column containing some data in 'date and time' format, e.g. >> >> > 'dd/mm/yyyy >> >> > hh:mm'. >> >> > >> >> > These data were automatically read and saved as 'factor' in R. When I >> >> > was >> >> > trying to produce some plots (such as time series) with the above >> >> > 'date >> >> > and >> >> > time' on x-axis, ?it caused some disodering problem, e.g. 1st of >> >> > March >> >> > 2012 >> >> > is in front of 10th of Feb. 2012 (if the data is from 10th Feb. 2012 >> >> > to >> >> > 1st >> >> > of March 2012). I understand that I might have to convert them from >> >> > 'factor' to 'date' first, so I tried using 'as.date'. But this method >> >> > seems >> >> > only work for data in format of ?'d/m/y' and no further option that >> >> > allows >> >> > me to add hours and minutes. >> >> > >> >> > I checked online for other methods such as 'as.POSIX' and 'strptime' >> >> > but >> >> > none of them seem to offer me a quick solution. >> >> > >> >> > Please note that the data I received is recorded every 10 minutes so >> >> > they >> >> > are saved in the form of ?'dd/mm/yyyy hh:mm', e.g. I only have data >> >> > measured up to 'minute' NOT to ?'second'. Are there any direct >> >> > solution >> >> > that I can solve this issue?? >> >> > >> >> > >> >> > Many thanks in advance! >> >> > HJ >> >> > >> >> > ? ? ? ?[[alternative HTML version deleted]] >> >> > >> >> >> >> > ______________________________________________ >> >> > 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. >> > >> > > >
Seemingly Similar Threads
- Importing multiple worksheets from one Excle/ csv file into R
- Read vector as multi-dimensional data in R by row
- How to combine conditional argument and logical argument in R to create subset of data...
- Check results between two data.frame
- Reading a bunch of csv files into R