RomainD
RomainD

Reputation: 97

Need help formatting date in R

I am trying to get a simple bar char of activity count by date; however, when I import my data into R, it either skipping some record or not properly converting the date format.

Here is the script I am using:

ua <- read.table('report_users_activities_byrole 2.txt',sep='|',header=T)
qplot(date, 
      data=ua, 
      geom="bar", 
      weight=count, 
      ylab="User Count", 
      fill=factor(un_region)) + 
    opts(axis.text.x =theme_text(angle=45, size=5))

And my date

    head(ua)

        date                 role                                         name   un_region                un_subregion               us_state count
1  2012-06-21   ENTREPRENEUR         Australia                                    Oceania     Australia and New Zealand                             2
2  2012-06-21   ENTREPRENEUR         Belgium                                      Europe      Western Europe                                        1
3  2012-06-21   ENTREPRENEUR         Bosnia and Herzegovina                       Europe      Southern Europe                                       1

Upvotes: 1

Views: 145

Answers (3)

Dirk is no longer here
Dirk is no longer here

Reputation: 368241

I suspect you need something like

 ua[,"Date"] <- as.Date(ua[,"Date"])

to turn the textual representation of the dates you got from reading the file into an actual Date type.

Upvotes: 3

RomainD
RomainD

Reputation: 97

Looks like i had some encoding issues with my data extract. I used Google refine to clean up the import and then ua <- read.csv("~/Desktop/R Working/report_users_activities_byrole.csv") and it worked

Upvotes: 0

Florian Oswald
Florian Oswald

Reputation: 5134

Not sure what's wrong with your code but something like this should work (that's a version of the example at http://had.co.nz/ggplot2/scale_date.html)

df = data.frame(date=sample(seq(Sys.Date(), len=100, by="1 day"),size=100,replace=TRUE))
qplot(x=date,data=df,geom="bar")

df is a data.frame where some dates appear more often than others (that's the sample() function). not sure why you want the "weight" argument in your qplot() call. Also make sure your date variable is a proper date (not a string), i.e. do

str(df$date)

otherwise

    qplot(x=factor(date),data=df,geom="bar")

should work as well.

Upvotes: 1

Related Questions