【问题标题】:Plotting the frequency of string matches over time in R在 R 中随着时间的推移绘制字符串匹配的频率
【发布时间】:2016-09-07 02:54:23
【问题描述】:

我已经编译了过去几个月左右发送的推文语料库,看起来像这样(实际的语料库有更多的列和显然更多的行,但你明白了)

id      when            time        day month   year    handle  what
UK1.1   Sat Feb 20 2016 12:34:02    20  2       2016    dave    Great goal by #lfc
UK1.2   Sat Feb 20 2016 15:12:42    20  2       2016    john    Can't wait for the weekend 
UK1.3   Sat Mar 01 2016 12:09:21    1   3       2016    smith   Generic boring tweet

现在我想在 R 中做的是,使用 grep 进行字符串匹配,绘制某些单词/主题标签随时间变化的频率,理想情况下通过该月/日/小时/任何时间的推文数量进行标准化。但我不知道该怎么做。

我知道如何使用 grep 创建此数据框的子集,例如对于所有推文,包括#lfc 标签,但我真的不知道从那里去哪里。

另一个问题是,无论我的 x 轴(小时/天/月等)上的时间尺度是什么,都必须是数字,而“何时”列则不是。我已经尝试将 2 月 13 日的“日”和“月”列连接成类似“2.13”的内容,但这会导致 R 将 2.13 视为“更早”的问题,可以这么说,比 2.7(2 月 7 日)以数学为基础。

所以基本上,I'd like to make plots like these, where frequency of string x is plotted against time

谢谢!

【问题讨论】:

  • 您似乎遇到了一些问题。谨慎的做法是将它们拆分为带有单独问题的较小问题。

标签: r plot time frequency


【解决方案1】:

这是计算每天推文的一种方法。我用一个简化的假数据集进行了说明:

library(dplyr)
library(lubridate)

# Fake data
set.seed(485)
dat = data.frame(time = seq(as.POSIXct("2016-01-01"),as.POSIXct("2016-12-31"), length.out=10000), 
                 what = sample(LETTERS, 10000, replace=TRUE))

tweet.summary = dat %>% group_by(day = date(time)) %>%  # To summarise by month: group_by(month = month(time, label=TRUE))
  summarise(total.tweets = n(),
            A.tweets = sum(grepl("A", what)),
            pct.A = A.tweets/total.tweets,
            B.tweets = sum(grepl("B", what)),
            pct.B = B.tweets/total.tweets)            

tweet.summary 
          day total.tweets A.tweets      pct.A B.tweets      pct.B
1  2016-01-01           28        3 0.10714286        0 0.00000000
2  2016-01-02           27        0 0.00000000        1 0.03703704
3  2016-01-03           28        4 0.14285714        1 0.03571429
4  2016-01-04           27        2 0.07407407        2 0.07407407
...

这是一种使用ggplot2 绘制数据的方法。我还使用 dplyrreshape2 包在 ggplot 中动态汇总了数据框:

library(ggplot2)
library(reshape2)
library(scales)

ggplot(dat %>% group_by(Month = month(time, label=TRUE)) %>%
         summarise(A = sum(grepl("A", what))/n(),
                   B = sum(grepl("B", what))/n()) %>%
         melt(id.var="Month"),
       aes(Month, value, colour=variable, group=variable)) +
  geom_line() +
  theme_bw() +
  scale_y_continuous(limits=c(0,0.06), labels=percent_format()) +
  labs(colour="", y="")

关于日期格式问题,以下是获取数字日期的方法:您可以使用 as.Date 将日月和年列转换为日期和/或将日、月、年和时间列转换为日期-使用as.POSIXct 的时间列。两者都将具有附加日期类的基础数值,因此 R 在绘图函数和其他函数中将它们视为日期。完成此转换后,您可以运行上面的代码以按天、月等计算推文。

# Fake time data
dat2 = data.frame(day=sample(1:28, 10), month=sample(1:12,10), year=2016, 
                  time = paste0(sample(c(paste0(0,0:9),10:12),10),":",sample(10:50,10)))

# Create date-time format column from existing day/month/year/time columns
dat2$posix.date = with(dat2, as.POSIXct(paste0(year,"-", 
                                         sprintf("%02d",month),"-", 
                                         sprintf("%02d", day)," ", 
                                         time)))

# Create date format column
dat2$date = with(dat2, as.Date(paste0(year,"-", 
                                      sprintf("%02d",month),"-", 
                                      sprintf("%02d", day))))

dat2
   day month year  time          posix.date       date
1   28    10 2016 01:44 2016-10-28 01:44:00 2016-10-28
2   22     6 2016 12:28 2016-06-22 12:28:00 2016-06-22
3    3     4 2016 11:46 2016-04-03 11:46:00 2016-04-03
4   15     8 2016 10:13 2016-08-15 10:13:00 2016-08-15
5    6     2 2016 06:32 2016-02-06 06:32:00 2016-02-06
6    2    12 2016 02:38 2016-12-02 02:38:00 2016-12-02
7    4    11 2016 00:27 2016-11-04 00:27:00 2016-11-04
8   12     3 2016 07:20 2016-03-12 07:20:00 2016-03-12
9   24     5 2016 08:47 2016-05-24 08:47:00 2016-05-24 
10  27     1 2016 04:22 2016-01-27 04:22:00 2016-01-27

通过执行as.numeric(dat2$posix.date),您可以看到 POSIXct 日期的基础值是数字(自 1970 年 1 月 1 日午夜以来经过的秒数)。同样对于 Date 对象(自 1970 年 1 月 1 日以来经过的天数):as.numeric(dat2$date)

【讨论】:

  • 非常感谢!这完美地工作。我遇到的唯一问题是,当我创建 posix 日期列(结合日期和时间)时,它仍然只给我日期。它实际上只是完全省略了时间,即使时间列是正确的格式/类。
  • 嗯...您能否发布一个您开始使用的数据的小样本以及您运行以将其转换为 POSIXct 的代码?要发布数据样本,请将dput(dataSample) 的输出粘贴到您的问题中。
  • 抱歉延迟了 - 我在一小部分数据上尝试了完全相同的代码,它运行良好,但它仍然不适用于整个数据集(即超过 1600 万行)。会不会是数据框大小的问题?我使用了你的代码,其中“年”、“月”和“日”都是整数,而“时间”是一个因素:data$datetime = with(data, as.POSIXct(paste0(year,"-", sprintf("%02d",month),"-", sprintf("%02d", day)," ", time))) 奇怪的是日期时间列 is 是正确的格式( "POSIXct" "POSIXt"),并打印为“2015-01-20 GMT”,但没有时间!
猜你喜欢
  • 2018-07-31
  • 2016-01-02
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多