Re: [R] Converting time format
Thank you. From: Rui Barradas Date: Monday, September 19, 2022 at 1:28 PM To: Parkhurst, David , R-help@r-project.org Subject: Re: [R] Converting time format Hello, I will run the examples below with the following data: x <- c("12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00") b <- data.frame(time = x, myvar = sin(2*pi*seq_along(x)/length(x))) Are they are saying is true, the vector b$myvar is a character vector and that's what is being displayed. In what follows I will first give examples of base graphics. The times are first coerced to a proper time class with package chron. library(chron) b$time <- as.times(b$time) # see ?plot.default for the meaning of # argument 'type' plot(myvar ~ time, b) plot(myvar ~ time, b, type = "l") plot(myvar ~ time, b, type = "b") With ggplot2, there is no need to load a date/time class package, R can do it with ?as.POSXct but the labels are datetime_breaks and datetime_labels. library(ggplot2) b |> dplyr::mutate(time = paste(Sys.Date(), time), time = as.POSIXct(time)) |> ggplot(aes(time, myvar)) + geom_line() + geom_point() + scale_x_datetime(date_breaks = "1 hour", date_labels = "%H:%M") + theme_bw() Hope this helps, Rui Barradas �s 01:56 de 19/09/2022, Parkhurst, David escreveu: > I have a dataframe obtained using read.csv from an excel file. Its first > column is times, running from 18:00 to 19:30. If I want to plot other > columns against time, do I need to convert those somehow, and how would I do > that? > > If I run plot(b$time,b$myvar) I get a decent plot, but a friend suggests that > R is just treating those numbers as text, and putting them in alphabetical > order. True? > >[[alternative HTML version deleted]] > > __ > R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Converting time format
Thank you. I�ll see what I can do with that. From: Rui Barradas Date: Monday, September 19, 2022 at 1:28 PM To: Parkhurst, David , r-help@r-project.org Subject: Re: [R] Converting time format Hello, I will run the examples below with the following data: x <- c("12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00") b <- data.frame(time = x, myvar = sin(2*pi*seq_along(x)/length(x))) Are they are saying is true, the vector b$myvar is a character vector and that's what is being displayed. In what follows I will first give examples of base graphics. The times are first coerced to a proper time class with package chron. library(chron) b$time <- as.times(b$time) # see ?plot.default for the meaning of # argument 'type' plot(myvar ~ time, b) plot(myvar ~ time, b, type = "l") plot(myvar ~ time, b, type = "b") With ggplot2, there is no need to load a date/time class package, R can do it with ?as.POSXct but the labels are datetime_breaks and datetime_labels. library(ggplot2) b |> dplyr::mutate(time = paste(Sys.Date(), time), time = as.POSIXct(time)) |> ggplot(aes(time, myvar)) + geom_line() + geom_point() + scale_x_datetime(date_breaks = "1 hour", date_labels = "%H:%M") + theme_bw() Hope this helps, Rui Barradas �s 01:56 de 19/09/2022, Parkhurst, David escreveu: > I have a dataframe obtained using read.csv from an excel file. Its first > column is times, running from 18:00 to 19:30. If I want to plot other > columns against time, do I need to convert those somehow, and how would I do > that? > > If I run plot(b$time,b$myvar) I get a decent plot, but a friend suggests that > R is just treating those numbers as text, and putting them in alphabetical > order. True? > >[[alternative HTML version deleted]] > > __ > R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Converting time format
Hello, I will run the examples below with the following data: x <- c("12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00") b <- data.frame(time = x, myvar = sin(2*pi*seq_along(x)/length(x))) Are they are saying is true, the vector b$myvar is a character vector and that's what is being displayed. In what follows I will first give examples of base graphics. The times are first coerced to a proper time class with package chron. library(chron) b$time <- as.times(b$time) # see ?plot.default for the meaning of # argument 'type' plot(myvar ~ time, b) plot(myvar ~ time, b, type = "l") plot(myvar ~ time, b, type = "b") With ggplot2, there is no need to load a date/time class package, R can do it with ?as.POSXct but the labels are datetime_breaks and datetime_labels. library(ggplot2) b |> dplyr::mutate(time = paste(Sys.Date(), time), time = as.POSIXct(time)) |> ggplot(aes(time, myvar)) + geom_line() + geom_point() + scale_x_datetime(date_breaks = "1 hour", date_labels = "%H:%M") + theme_bw() Hope this helps, Rui Barradas Às 01:56 de 19/09/2022, Parkhurst, David escreveu: I have a dataframe obtained using read.csv from an excel file. Its first column is times, running from 18:00 to 19:30. If I want to plot other columns against time, do I need to convert those somehow, and how would I do that? If I run plot(b$time,b$myvar) I get a decent plot, but a friend suggests that R is just treating those numbers as text, and putting them in alphabetical order. True? [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Converting time format
What is the output of > class(b$time) ? Also, start sending your emails in plaint text format, if possible. On Mon, Sep 19, 2022 at 2:12 PM Parkhurst, David wrote: > > I have a dataframe obtained using read.csv from an excel file. Its first > column is times, running from 18:00 to 19:30. If I want to plot other > columns against time, do I need to convert those somehow, and how would I do > that? > > If I run plot(b$time,b$myvar) I get a decent plot, but a friend suggests that > R is just treating those numbers as text, and putting them in alphabetical > order. True? > > [[alternative HTML version deleted]] > > __ > R-help@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. __ R-help@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.
[R] Converting time format
I have a dataframe obtained using read.csv from an excel file. Its first column is times, running from 18:00 to 19:30. If I want to plot other columns against time, do I need to convert those somehow, and how would I do that? If I run plot(b$time,b$myvar) I get a decent plot, but a friend suggests that R is just treating those numbers as text, and putting them in alphabetical order. True? [[alternative HTML version deleted]] __ R-help@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.
Re: [R] converting time format
Sure: df.count.mon$time<-format(as.Date(paste(df.count.mon$year, df.count.mon$mon,1), '%Y %m %d'),"%Y %m") > df.count.mon count year montime 1 22 2014 1 2014 01 2 12 2014 2 2014 02 ... Jim On Thu, Feb 23, 2017 at 8:18 AM, lily li wrote: > Yes, it is a little different. Is there a way to get '-mm' format? > Thanks. > > On Wed, Feb 22, 2017 at 2:10 PM, Jim Lemon wrote: >> >> Hi Lily. >> Two problems. You have named the month field "mon" and then refer to >> it as "month". Second, as the resolution of as.Date is days, it can't >> produce a valid date without specifying the day. Thus: >> >> df.count.mon<-data.frame(count=sample(1:24,24), >> year=rep(2014:2015,each=2),mon=rep(1:12,2)) >> # make each day the first day of the month >> df.count.mon$time<- >> as.Date(paste(df.count.mon$year, df.count.mon$mon,1), >> '%Y %m %d') >> df.count.mon >>count year mon time >> 1 22 2014 1 2014-01-01 >> 2 12 2014 2 2014-02-01 >> ... >> You will get values, but I don't think they are the ones you want. >> >> Jim >> >> On Thu, Feb 23, 2017 at 6:33 AM, lily li wrote: >> > Hi R users, >> > >> > I have a dataframe, with year, month, day, and other variables. I wanted >> > to >> > calculated monthly values of the variables. For example, there is one >> > variable called 'count'. I use the code below to convert daily data to >> > monthly data. >> > >> > df.count.mon = aggregate(count ~ year+month, data= df, sum) >> > >> > The new dataframe has three columns: year, month, and count. Now I want >> > to >> > add one more column as 'time', which has the format '-mm'. I use the >> > code below but the new column has all NA values. What is the problem and >> > how to solve it? >> > >> > df.count.mon$time = as.Date(paste(df.count.mon$year, >> > df.count.mon$month), >> > '%Y %m') >> > >> > I had experience to add one more column with the format '-mm-dd', >> > which >> > works, but not with monthly format. Thanks for your help. >> > >> > [[alternative HTML version deleted]] >> > >> > __ >> > R-help@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. > > __ R-help@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.
Re: [R] converting time format
Yes, it is a little different. Is there a way to get '-mm' format? Thanks. On Wed, Feb 22, 2017 at 2:10 PM, Jim Lemon wrote: > Hi Lily. > Two problems. You have named the month field "mon" and then refer to > it as "month". Second, as the resolution of as.Date is days, it can't > produce a valid date without specifying the day. Thus: > > df.count.mon<-data.frame(count=sample(1:24,24), > year=rep(2014:2015,each=2),mon=rep(1:12,2)) > # make each day the first day of the month > df.count.mon$time<- > as.Date(paste(df.count.mon$year, df.count.mon$mon,1), > '%Y %m %d') > df.count.mon >count year mon time > 1 22 2014 1 2014-01-01 > 2 12 2014 2 2014-02-01 > ... > You will get values, but I don't think they are the ones you want. > > Jim > > On Thu, Feb 23, 2017 at 6:33 AM, lily li wrote: > > Hi R users, > > > > I have a dataframe, with year, month, day, and other variables. I wanted > to > > calculated monthly values of the variables. For example, there is one > > variable called 'count'. I use the code below to convert daily data to > > monthly data. > > > > df.count.mon = aggregate(count ~ year+month, data= df, sum) > > > > The new dataframe has three columns: year, month, and count. Now I want > to > > add one more column as 'time', which has the format '-mm'. I use the > > code below but the new column has all NA values. What is the problem and > > how to solve it? > > > > df.count.mon$time = as.Date(paste(df.count.mon$year, > df.count.mon$month), > > '%Y %m') > > > > I had experience to add one more column with the format '-mm-dd', > which > > works, but not with monthly format. Thanks for your help. > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] converting time format
Hi Lily. Two problems. You have named the month field "mon" and then refer to it as "month". Second, as the resolution of as.Date is days, it can't produce a valid date without specifying the day. Thus: df.count.mon<-data.frame(count=sample(1:24,24), year=rep(2014:2015,each=2),mon=rep(1:12,2)) # make each day the first day of the month df.count.mon$time<- as.Date(paste(df.count.mon$year, df.count.mon$mon,1), '%Y %m %d') df.count.mon count year mon time 1 22 2014 1 2014-01-01 2 12 2014 2 2014-02-01 ... You will get values, but I don't think they are the ones you want. Jim On Thu, Feb 23, 2017 at 6:33 AM, lily li wrote: > Hi R users, > > I have a dataframe, with year, month, day, and other variables. I wanted to > calculated monthly values of the variables. For example, there is one > variable called 'count'. I use the code below to convert daily data to > monthly data. > > df.count.mon = aggregate(count ~ year+month, data= df, sum) > > The new dataframe has three columns: year, month, and count. Now I want to > add one more column as 'time', which has the format '-mm'. I use the > code below but the new column has all NA values. What is the problem and > how to solve it? > > df.count.mon$time = as.Date(paste(df.count.mon$year, df.count.mon$month), > '%Y %m') > > I had experience to add one more column with the format '-mm-dd', which > works, but not with monthly format. Thanks for your help. > > [[alternative HTML version deleted]] > > __ > R-help@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. __ R-help@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.
[R] converting time format
Hi R users, I have a dataframe, with year, month, day, and other variables. I wanted to calculated monthly values of the variables. For example, there is one variable called 'count'. I use the code below to convert daily data to monthly data. df.count.mon = aggregate(count ~ year+month, data= df, sum) The new dataframe has three columns: year, month, and count. Now I want to add one more column as 'time', which has the format '-mm'. I use the code below but the new column has all NA values. What is the problem and how to solve it? df.count.mon$time = as.Date(paste(df.count.mon$year, df.count.mon$month), '%Y %m') I had experience to add one more column with the format '-mm-dd', which works, but not with monthly format. Thanks for your help. [[alternative HTML version deleted]] __ R-help@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.