I've managed to confuse myself to a standstill when it comes to aggregating or binning a zoo object in R because I'm new to working with R and in particular working with time series data.
Can anyone help me out?
I have a number of dataframes which gives the creation dates of a tweets and its ID for a number of specific twitter accounts
str(temp)
'data.frame': 1528 obs. of 2 variables:
$ id_str : chr "605698007263260672" "605681239408963584" "603854670856069120" "601792133297786880" ...
$ created_at: POSIXct, format: "2015-06-02 12:30:32" "2015-06-02 11:23:55" "2015-05-28 10:25:47" "2015-05-22 17:49:59" ...
I don't know how frequent the tweets were (the spacing between creation date values) but I then need to create a dataset which contains
TimeSeries AccountName NumOfTweets 2010-01 MyTweeter 45 2010-02 YourTweeter 5
I would like to group according to the week or month created and count how many there were and plot them to show how a number of accounts compare to each other in number of tweets and sustained activity since records began.
Any advice on how to handle merging or joining time series so I can plot them with the time series on the x axis and the number of tweets on the Y
Random sample of observations taken using select_n() and provided below using dput
dput(sample.df)
structure(list(id_str = c("235710687006035968", "148522094328680448",
"555743466945523712", "139818931253813249", "601792133297786880",
"391194341978669057", "455754624859779072", "139640022696603648",
"182085980864528384", "372375117130526720"), created_at = structure(c(1345032781,
1324245401, 1421334542, 1322170405, 1432313399, 1382102973, 1397495344,
1322127750, 1332247655, 1377616120), class = c("POSIXct", "POSIXt"
), tzone = "")), .Names = c("id_str", "created_at"), row.names = c(882L,
1363L, 33L, 1478L, 4L, 536L, 180L, 1489L, 1116L, 635L), class = "data.frame")
Example of desired output but need help in calculating the aggregate and merging multiple dataframes (1 per Account) into a suitable end data structure for plotting
Does this resemble what you are looking for? First, convert created_at
to monthly and count the observations (tweets) by ID and month:
# To have some counts > 1 and several observations per ID
set.seed(123)
df2 <- data.frame(sample(df$id_str, size = 50, replace = T),
sample(df$created_at, size = 50, replace = T))
colnames(df2) <- colnames(df)
# Convert to months
df2$Month <- strftime(df2$created_at, format = "%Y-%m")
result <- aggregate(df2$id_str, by = list(df2$id_str, df2$Month), FUN = length)
colnames(result) <- c("ID", "Month", "nTweets")
head(result)
# ID Month nTweets
# 1 139640022696603648 2011-11 1
# 2 139818931253813249 2011-11 1
# 3 148522094328680448 2011-11 1
# 4 182085980864528384 2011-11 2
# 5 391194341978669057 2011-11 1
# 6 455754624859779072 2011-11 2
Then you can plot the result for example using ggplot:
library(ggplot2)
ggplot(result, aes(x = Month, y = nTweets, group = ID, color = ID)) +
geom_line(size = 2)
Note that the x-axis is not correctly spaced here because some months have no observations. I suppose this is not true for the complete data.
Following Khl4v's code and a bit of trial and error
Firstly Convert the char column "created_at" to a Date object using the required formatting string so it can be recognised as a date value
MyDataFrame <- mutate(MyDataFrame,created_at = as.POSIXct(created_at, format="%a %b %d %H:%M:%S %z %Y"))
Now convert it to the Year-Month value before creating a new dataframe called df2 with a character string "Tweets" we will shortly count next as the year-month value changes
df2 <- data.frame("Tweets",strftime(MyDataFrame$created_at, format = "%Y-%m"))
Rename the column names to be something more user friendly
colnames(df2) <- c("Tweeter","TimePeriod") Count using the aggregate function the number/length of times in columnd Tweeter for each change in the column value of TimePeriod
result <- aggregate(df2$Tweeter, by = list(df2$TimePeriod), FUN = length)
Add another column to the result to store the name of the tweeter account used
result <- mutate(result ,Account ="MyTwitter")
Rename the column names to be more user friendly
colnames(result) <- c("TimePeriod","Tweets","Tweeter")
plot the result using ggplot and rotate the x labels so they are a bit easier to read
ggplot(result, aes(x = TimePeriod, y = Tweets, group = Tweeter, color = Tweeter)) + geom_line(size = 1) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.