现在时间和发言人信息都读到 data 的 time 和 id 中了,可以确认下提取内容:data、data
id、datatime。
还没完,时间还是字符串,还需要继续处理:
数据整理 将字符串中的日期和时间划分为不同变量
temp1 <- str_split(data$time,0); line-height: 1.5 !important;">' ')
result1 <- ldply(temp1,.fun=NULL)
names(result1) <- c(date',0); line-height: 1.5 !important;">clock')分离年月日
temp2 <- str_split(result1$date,0); line-height: 1.5 !important;">-')
result2 <- ldply(temp2,.fun=NULL)
names(result2) <- c(yearmonthday 分离小时分钟temp3 <- str_split(result1$clock,0); line-height: 1.5 !important;">:')
result3 <- ldply(temp3,.fun=NULL)
names(result3) <- c(hourminutessecond 合并数据
newdata <- cbind(data,result1,result2,result3)
转换日期为时间格式
newdata$date <- ymd(newdata$date)
提取星期数据
newdata$wday <- wday(newdata$date) 转换数据格式newdata$month <- ordered(as.numeric(newdata$month) )
newdata$year <- ordered(newdata$year)
newdata$day <- ordered(as.numeric(newdata$day))
newdata$hour <- ordered(as.numeric(newdata$hour))
newdata$wday <- ordered(newdata$wday)
至此,数据预处理完成,时间和发言人数据都已合适地存到 newdata 中,可以开始任性地分析了~
3、数据分析
qplot(wday,data=newdata,geom=bar')
周三是工作日,还这么活跃,周六话最多,周日估计出去玩了,周一专心上班。
qplot(hour,255);">这群一天中聊得最嗨的是上午 10 点和下午 17 点,形成两个高峰。
user <- as.data.frame(table(newdata$id)) 用 table 统计频数
user <- user[order(user$Freq,decreasing=T),]
user[1:10,] 显示前十大发言人的 ID 和 发言次数
topuser <- user[1:10,]$Var1 存前十大发言人的 ID
活跃天数计算 将数据展开为宽表,每一行为用户,每一列为日期,对应数值为发言次数
flat.day <- dcast(newdata,id~date,length,value.var=')
flat.mat <- as.matrix(flat.day[-1]) 转为矩阵 转为0-1值,以观察是否活跃
flat.mat <- ifelse(flat.mat>0,1,0) 根据上线天数求和
topday <- data.frame(flat.day[,1],apply(flat.mat,sum))
names(topday) <- c(iddays')
topday <- topday[order(topday$days,] 获得前十大活跃用户topday[1:10,]
?
|