簡體   English   中英

在R中繪制字符串隨時間變化的頻率

[英]Plotting the frequency of string matches over time in R

我已經整理了過去幾個月左右發送的推文的語料庫,看起來像這樣(實際語料庫有更多的列,顯然還有更多的行,但是您知道了)

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主題標簽的所有tweet,但我真的不知道從那里去哪里。

另一個問題是,無論我的x軸上的時間標度是什么(小時/天/月等),都需要數字化,而“時間”列則不需要。 我嘗試將2月13日的“日”和“月”列連接為類似“ 2.13”的內容,但這導致R將2.13視為比2.7(“ 2月7日”)“更早”的問題。基於數學依據。

所以基本上, 我想繪制這樣的圖,其中將字符串x的頻率與時間作圖

謝謝!

這是一種按天計算推文的方法。 我用一個簡化的偽數據集進行了說明:

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包對dplyr的數據幀進行了總結:

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.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日午夜以來經過的as.numeric(dat2$posix.date) 同樣對於Date對象(自1970年1月1日起經過的天數): as.numeric(dat2$date)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM