在R中需要帮助格式化日期

时间:2012-09-21 16:19:24

标签: r ggplot2

我正在尝试按日期获取活动计数的简单条形码;但是,当我将数据导入R时,它会跳过某些记录或者没有正确转换日期格式。

这是我正在使用的脚本:

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))

我的日期

    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

3 个答案:

答案 0 :(得分:3)

我怀疑你需要像

这样的东西
 ua[,"Date"] <- as.Date(ua[,"Date"])

将阅读文件的日期的文本表示形式转换为实际的日期类型。

答案 1 :(得分:1)

不确定您的代码有什么问题,但这样的事情应该有用(这是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是一个data.frame,其中某些日期比其他日期更频繁出现(即sample()函数)。不确定为什么要在qplot()调用中使用“weight”参数。还要确保您的日期变量是正确的日期(不是字符串),即执行

str(df$date)

否则

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

应该也可以。

答案 2 :(得分:0)

看起来我的数据提取有一些编码问题。我使用Google精简来清理导入然后 ua <- read.csv("~/Desktop/R Working/report_users_activities_byrole.csv")并且有效

相关问题