Ebert,Timothy Aaron
2022-Jun-15 02:01 UTC
[R] How do I "teach" R that columns 1, 2, and 3 are the Year-Month-Day
You don't, or not exactly. You could make the three columns strings, then add them together. I would then use the lubridate package to convert to a date. The first four lines of code are just getting a bit of data into R. The lubridate package will also work with date-time objects if you have time. year<-rep("2021",4) month<-c(7,8,9,11) day<-c(25,27,26,4) fuel<-c(50.45, 61.48,59.07,55.40) df<-data.frame(year,month,day,fuel) df$day <-as.character(df$day) df$month<-as.character(df$month) df$date <- paste(year,"/",month,"/",day) library(lubridate) df$date<-ymd(df$date) Regards, Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Avi Gross via R-help Sent: Tuesday, June 14, 2022 10:00 PM To: r-help at r-project.org Cc: r-help at r-project.org Subject: Re: [R] How do I "teach" R that columns 1, 2, and 3 are the Year-Month-Day [External Email] Greg, It looks like you mean to have a data.frame containing the year as a column then Month, Day and something called Fuel. You only made a vector called Fuel, with no name as part of it and plotted it. So you did not expect any names, I assume. You probably want to combine the first three columns into holding some kind of date object with something like as.Date("2021-07-25") for each row and make a vector of those. Your data.frame might then contain two columns called Date and Fuel and you might use whatever plotting method to put Fuel on the Y axis and dates on the X axis or whatever makes sense. Plotting four independent columns will not work here. And note the NAMES go in with something like: mydata <- data.frame(Date=dates, Fuel=Fuel) I have not done all the work but hopefully this is helpful. -----Original Message----- From: Gregory Coats via R-help <r-help at r-project.org> To: Greg Comcast Coats <gregcoats at me.com> Cc: Marc Schwartz via R-help <r-help at r-project.org> Sent: Tue, Jun 14, 2022 9:08 pm Subject: [R] How do I "teach" R that columns 1, 2, and 3 are the Year-Month-Day # Column 1 is the Year # Column 2 is the Month # Column 3 is the Day # Column 4 is the Fuel Fuel <- c(50.45, 61.48, 59.07, 55.40, 30.63, 41.35, 32.81, 49.86, 62.99, 89.37) plot (Fuel) 2021 7 25 50.45 2021 8 27 61.48 2021 9 26 59.07 2021 11 4 55.40 2021 11 22 30.63 2021 11 26 41.35 2021 12 6 32.81 2022 1 14 49.86 2022 4 29 62.99 2022 6 11 89.37 How do I "teach" R that columns 1, 2, and 3 are the Year-Month-Day? Greg Coats [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=5mwGPy3wD6UgdaNamboKRB8Oo4Yd9OIBPIhrRkniM0N0wUSsekHwK4HbHytbdscH&s=3iFRCZeUCCgixeFjZsrmkHARgeHp8CLb7dIAk7EHiSY&ePLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=5mwGPy3wD6UgdaNamboKRB8Oo4Yd9OIBPIhrRkniM0N0wUSsekHwK4HbHytbdscH&s=BG0mynWESmDXi-6Z2LX7yRr51Gqw3B-Gzfg3Ye2cy8M&eand provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=5mwGPy3wD6UgdaNamboKRB8Oo4Yd9OIBPIhrRkniM0N0wUSsekHwK4HbHytbdscH&s=3iFRCZeUCCgixeFjZsrmkHARgeHp8CLb7dIAk7EHiSY&ePLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=5mwGPy3wD6UgdaNamboKRB8Oo4Yd9OIBPIhrRkniM0N0wUSsekHwK4HbHytbdscH&s=BG0mynWESmDXi-6Z2LX7yRr51Gqw3B-Gzfg3Ye2cy8M&eand provide commented, minimal, self-contained, reproducible code.
Martin Maechler
2022-Jun-15 10:02 UTC
[R] How do I "teach" R that columns 1, 2, and 3 are the Year-Month-Day
>>>>> Ebert,Timothy Aaron >>>>> on Wed, 15 Jun 2022 02:01:44 +0000 writes:> You don't, or not exactly. You could make the three > columns strings, then add them together. I would then use > the lubridate package to convert to a date. The first > four lines of code are just getting a bit of data into > R. The lubridate package will also work with date-time > objects if you have time. > year<-rep("2021",4) > month<-c(7,8,9,11) > day<-c(25,27,26,4) > fuel<-c(50.45, 61.48,59.07,55.40) > df<-data.frame(year,month,day,fuel) > df$day <-as.character(df$day) > df$month<-as.character(df$month) > df$date <- paste(year,"/",month,"/",day) > library(lubridate) > df$date<-ymd(df$date) > Regards, > Tim Thank you, but Jeff Newmiller's answer which was much shorter *and* only used base R instead of an extra package was much more convincing to me. Martin > -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Avi Gross via R-help > Sent: Tuesday, June 14, 2022 10:00 PM > To: r-help at r-project.org > Cc: r-help at r-project.org > Subject: Re: [R] How do I "teach" R that columns 1, 2, and 3 are the Year-Month-Day > [External Email] > Greg, > It looks like you mean to have a data.frame containing the year as a column then Month, Day and something called Fuel. > You only made a vector called Fuel, with no name as part of it and plotted it. > So you did not expect any names, I assume. > You probably want to combine the first three columns into holding some kind of date object with something like as.Date("2021-07-25") for each row and make a vector of those. Your data.frame might then contain two columns called Date and Fuel and you might use whatever plotting method to put Fuel on the Y axis and dates on the X axis or whatever makes sense. Plotting four independent columns will not work here. > And note the NAMES go in with something like: > mydata <- data.frame(Date=dates, Fuel=Fuel) I have not done all the work but hopefully this is helpful. > -----Original Message----- > From: Gregory Coats via R-help <r-help at r-project.org> > To: Greg Comcast Coats <gregcoats at me.com> > Cc: Marc Schwartz via R-help <r-help at r-project.org> > Sent: Tue, Jun 14, 2022 9:08 pm > Subject: [R] How do I "teach" R that columns 1, 2, and 3 are the Year-Month-Day > # Column 1 is the Year > # Column 2 is the Month > # Column 3 is the Day > # Column 4 is the Fuel > Fuel <- c(50.45, 61.48, 59.07, 55.40, 30.63, 41.35, 32.81, 49.86, 62.99, 89.37) plot (Fuel) > 2021 7 25 50.45 > 2021 8 27 61.48 > 2021 9 26 59.07 > 2021 11 4 55.40 > 2021 11 22 30.63 > 2021 11 26 41.35 > 2021 12 6 32.81 > 2022 1 14 49.86 > 2022 4 29 62.99 > 2022 6 11 89.37 > How do I "teach" R that columns 1, 2, and 3 are the Year-Month-Day? > Greg Coats > [[alternative HTML version deleted]] > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=5mwGPy3wD6UgdaNamboKRB8Oo4Yd9OIBPIhrRkniM0N0wUSsekHwK4HbHytbdscH&s=3iFRCZeUCCgixeFjZsrmkHARgeHp8CLb7dIAk7EHiSY&e > PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=5mwGPy3wD6UgdaNamboKRB8Oo4Yd9OIBPIhrRkniM0N0wUSsekHwK4HbHytbdscH&s=BG0mynWESmDXi-6Z2LX7yRr51Gqw3B-Gzfg3Ye2cy8M&e > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=5mwGPy3wD6UgdaNamboKRB8Oo4Yd9OIBPIhrRkniM0N0wUSsekHwK4HbHytbdscH&s=3iFRCZeUCCgixeFjZsrmkHARgeHp8CLb7dIAk7EHiSY&e > PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=5mwGPy3wD6UgdaNamboKRB8Oo4Yd9OIBPIhrRkniM0N0wUSsekHwK4HbHytbdscH&s=BG0mynWESmDXi-6Z2LX7yRr51Gqw3B-Gzfg3Ye2cy8M&e > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ > 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.