0

I have a .csv file that I have loaded into R using the following basic command:

lace <- read.csv("lace for R.csv")

It pulls in my data just fine. Here is the str of the data:

str(lace)
'data.frame':   2054 obs. of  20 variables:
 $ Admission.Day       : Factor w/ 872 levels "1/1/2013","1/10/2011",..: 231 238 238 50 59 64 64 64 67 67 ...
 $ Year                : int  2010 2010 2010 2011 2011 2011 2011 2011 2011 2011 ...
 $ Month               : int  12 12 12 1 1 1 1 1 1 1 ...
 $ Day                 : int  28 30 30 3 4 6 6 6 7 7 ...
 $ DayOfWeekNumber     : int  3 5 5 2 3 5 5 5 6 6 ...
 $ Day.of.Week         : Factor w/ 7 levels "Friday","Monday",..: 6 5 5 2 6 5 5 5 1 1 ...

What I am trying to do is create three (3) different histograms and then plot them all together on one. I want to create a histogram for each year, where the x axis or labels will be the days of the week starting with Sunday and ending on Saturday.

Firstly how would I go about creating a histogram out of Factors, which the days of the week are in?

Secondly how do I create a histogram for the days of the week for a given year?

I have tried using the following post here but cannot get it working. I use the Admission.Day as the variable and get an error message:

dat <- as.Date(lace$Admission.Day)

Error in charToDate(x) : character string is not in a standard unambiguous format

Thank you,

Community
  • 1
  • 1
MCP_infiltrator
  • 3,961
  • 10
  • 45
  • 82
  • 1
    The first thing you need to do is read the documentation for `?as.Date`, in particular the description for the `format` argument. Next, you'll want to look at the function `?weekdays`. – joran Nov 27 '13 at 17:32
  • 1
    `as.Date` needs an origin `dat <- as.Date(lace$Admission.Day, origin="1970-01-01")` – Ricardo Saporta Nov 27 '13 at 17:32
  • @joran got all the dates now, thank you for pointing out the format part, not having an origin as specified by excel was really throwing everything off. – MCP_infiltrator Nov 27 '13 at 18:00
  • weird I do the same thing three times over and one of the files does not work – MCP_infiltrator Nov 27 '13 at 19:10
  • also to get the dates that I want I am doing something like this: `radf <- data.frame(lace[lace$FAILURE == 1,])` – MCP_infiltrator Nov 27 '13 at 19:15
  • @joran if you want to put that as your answer I'll mark it as accepted, I was able to take part of the second question – MCP_infiltrator Nov 27 '13 at 19:37
  • 1
    Sounds like your problem is with importing dates, rather than making histograms. If your data starts out in Excel, I'd strongly recommend you consider the `XLConnect` package, especially `loadWorkbook(...)` and `readWorksheet(...)`. In my experience, this package does a much better job interpreting Excel dates. See the documentation [here](http://cran.r-project.org/web/packages/XLConnect/index.html). – jlhoward Nov 27 '13 at 21:31

1 Answers1

1

Expanding on the comment above: the problem seems to be with importing dates, rather than making the histogram. Assuming there is an excel workbook "lace for R.xlsx", with a sheet "lace":

## Not tested...
library(XLConnect)
myData <- "lace for R.xlsx"             # NOTE: need path also...
wb     <- loadWorkbook(myData)
lace   <- readWorksheet(wb, sheet="lace")
lace$Admission.Day <- as.Date(lace$Admission.Day)

should provide dates that work with all the R date functions. Also, the lubridate package provides a number of functions that are more intuitive to use than format(...).

Then, as an example:

library(lubridate)   # for year(...) and wday(...)
library(ggplot2)
# random dates around Jun 1, across 5 years...
set.seed(123)
lace <- data.frame(date=as.Date(rnorm(1000,sd=50)+365*(0:4),origin="2008/6/1"))
lace$year <- factor(year(lace$date))
lace$dow  <- wday(lace$date, label=T)
# This creates the histograms...
ggplot(lace) +
  geom_histogram(aes(x=dow, fill=year)) +      # fill color by year
  facet_grid(~year) +                          # facet by year
  theme(axis.text.x=element_text(angle=90))    # to rotate weekday names...

Produces this: enter image description here

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Anyone trying to get this to work might like to try ```geom_bar(aes(x=dow, fill=year))``` to replace ```geom_histogram(aes(x=dow, fill=year))``` – Big Old Dave Nov 26 '19 at 09:03