当前位置 : 主页 > 手机开发 > 其它 >

帮助R和分组/聚合/ * apply / data.table

来源:互联网 收集:自由互联 发布时间:2021-06-22
我是R的新手,无法运行功能来获得我需要的答案.我有示例数据PCSTest http://pastebin.com/z9Ti3nHB 看起来像这样: Date Site Word--------------------------------------9/1/2012 slashdot javascript9/1/2012 stackexchan
我是R的新手,无法运行功能来获得我需要的答案.我有示例数据PCSTest

http://pastebin.com/z9Ti3nHB

看起来像这样:

Date        Site            Word
--------------------------------------
9/1/2012    slashdot        javascript
9/1/2012    stackexchange   R
9/1/2012    reddit          R
9/1/2012    slashdot        javascript
9/1/2012    stackexchange   javascript
9/5/2012    reddit          R
9/8/2012    slashdot        javascript
9/8/2012    stackexchange   R
9/8/2012    reddit          R
9/8/2012    slashdot        javascript
9/18/2012   stackexchange   R
9/18/2012   reddit          R
9/18/2012   slashdot        javascript
9/18/2012   stackexchange   R
9/27/2012   reddit          R
9/27/2012   slashdot        R

我的目标是随着时间的推移寻找与网站相关的不同单词出现的趋势.我可以算一下:

library(plyr)   
PCSTest <- read.csv(file="c:/PCS/PCS Data - Test.csv", header=TRUE)
PCSTest$Date <- as.Date(PCSTest$Date, "%m/%d/%Y")
PCSTest$Date <- as.POSIXct(PCSTest$Date)
countTest <- count(PCSTest, c("Date", "Site", "Word"))

这给了这个:

Date          Site       Word freq
1  2012-08-31 20:00:00        reddit          R    4
2  2012-08-31 20:00:00      slashdot javascript    7
3  2012-08-31 20:00:00 stackexchange javascript    1
4  2012-08-31 20:00:00 stackexchange          R    2
5  2012-09-01 20:00:00        reddit javascript    2
6  2012-09-01 20:00:00      slashdot          R    3
7  2012-09-04 20:00:00        reddit          R    1
8  2012-09-07 20:00:00        reddit          R    1
9  2012-09-07 20:00:00      slashdot javascript    2
10 2012-09-07 20:00:00 stackexchange          R    1
11 2012-09-09 20:00:00 stackexchange javascript    4
12 2012-09-10 20:00:00      slashdot          R    4
13 2012-09-14 20:00:00        reddit javascript    4
14 2012-09-17 20:00:00        reddit          R    4
15 2012-09-17 20:00:00      slashdot javascript    1
16 2012-09-17 20:00:00 stackexchange          R    2
17 2012-09-19 20:00:00        reddit javascript    2
18 2012-09-23 20:00:00 stackexchange javascript    2
19 2012-09-24 20:00:00        reddit javascript    3
20 2012-09-24 20:00:00 stackexchange javascript    1
21 2012-09-24 20:00:00 stackexchange          R    4
22 2012-09-25 20:00:00        reddit javascript    5
23 2012-09-25 20:00:00      slashdot javascript    3
24 2012-09-25 20:00:00 stackexchange          R    7
25 2012-09-26 20:00:00        reddit          R    1
26 2012-09-26 20:00:00      slashdot          R    5

或将它们全部绘制成:

library(ggplot2)
ggplot(data=countTest, aes(x=Date, y=freq, group=interaction(Site, Word), colour=interaction(Site, Word), shape=Site)) + geom_line() + geom_point()

我现在需要对数据进行一些计算,所以我尝试了聚合

aggregate(freq ~ Site + Word, data = countTest,  function(freq) cbind(mean(freq), max(freq)))[order(-agg$freq[,3]),]

这使:

Site       Word freq.1 freq.2
2      slashdot javascript   3.25   7.00
5      slashdot          R   4.00   5.00
1        reddit javascript   3.20   5.00
4        reddit          R   2.20   4.00
6 stackexchange          R   3.20   7.00
3 stackexchange javascript   2.00   4.00

在最后一个结果中我想要的是一个具有每天平均频率的列,例如…… sum(freq)/ 20天,根据数据计算,甚至可能是移动平均值.
另外,我想要另一个具有斜率/线性回归的列.我如何计算聚合函数?

或者,我如何更好/更快地做出这些?我知道有apply和data.table函数,但我不知道如何使用它们.任何帮助将不胜感激!

我不确定你想要做什么,但是dplyr(或者plyr)会帮助你.
这是一个例子.如果你明确告诉你想要什么,你将获得更多帮助.

d <- read.csv("~/Downloads/r_data.txt")
d$Date <- as.POSIXct(as.Date(d$Date, "%m/%d/%Y"))

library(dplyr)
d.cnt <- d %>% group_by(Date, Site, Word) %>% summarise(cnt = n())

# average per day
date.range <- d$Date %>% range %>% diff %>% as.numeric # gives 26 days or
date.range <- d$Date %>% unique %>% length # gives 13 days
d.ave <- d.cnt %>% group_by(Site, Word) %>% summarize(ave_per_day = sum(cnt)/date.range)

# slope
d.reg <- d.cnt %>% group_by(Site, Word) %>% 
  do({fit = lm(cnt ~ Date, data = .); data.frame(int = coef(fit)[1], slope = coef(fit)[2])})

# plot the slope value
library(ggplot2)
ggplot(d.reg, aes(Site, slope, fill = Word)) + geom_bar(stat = "identity", position = "dodge")
网友评论