我想找到每个用户播放歌曲的平均时间,给出不同用户的歌曲播放时间列表。
user time action
A 2013-03-25T14:12:24Z PLAY
B 2013-03-28T14:54:30Z LIKE
C 2013-04-18T18:51:10Z LIKE
D 2013-05-07T18:06:24Z PLAY
B 2013-04-23T12:18:41Z PLAY
D 2013-04-29T12:00:16Z PLAY
A 2013-03-27T12:09:37Z PLAY
A 2013-04-16T18:31:44Z PLAY
我只想包括行动等于播放的时间。
提前致谢
答案 0 :(得分:2)
以下代码返回每个用户播放歌曲的平均小时(当天):
DF <-
read.csv(text=
"user,time,action
A,2013-03-25T14:12:24Z,PLAY
B,2013-03-28T14:54:30Z,LIKE
C,2013-04-18T18:51:10Z,LIKE
D,2013-05-07T18:06:24Z,PLAY
B,2013-04-23T12:18:41Z,PLAY
D,2013-04-29T12:00:16Z,PLAY
A,2013-03-27T12:09:37Z,PLAY
A,2013-04-16T18:31:44Z,PLAY",stringsAsFactors=F)
# filter by PLAY
plays <- DF[DF$action == "PLAY",]
# create means by user
byRes <-
by(plays, plays$user,
FUN=function(grp){
dates <- as.POSIXlt(grp$time,format="%Y-%m-%dT%H:%M:%S", tz = "GMT")
data.frame(user=grp$user[1],AvgHour=mean(dates$hour))
})
# put the "by" result into a data.frame
res <- do.call(rbind,byRes)
# result :
> res
user AvgHour
A A 14.66667
B B 12.00000
D D 15.00000