-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask3.R
More file actions
244 lines (191 loc) · 8.05 KB
/
Copy pathTask3.R
File metadata and controls
244 lines (191 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
install.packages("sqldf")
install.packages("microbenchmark")
install.packages("data.table")
library(sqldf)
library(data.table)
#installing packages
#check function - checking every field of data frame, if one is false then result is false
check <- function(a){
c = all(any(a==FALSE))
r= all(any(a==FALSE,2))
return (!(c&r))
}
#changing directory
getwd()
setwd("/Users/Szymon/Desktop/rstudio")
# loading csv files to data frames
Badges<-read.csv(file = 'badges.csv')
Users<-read.csv(file = 'Users.csv')
Posts<-read.csv(file = 'Posts.csv')
Votes<-read.csv(file = 'Votes.csv')
# setting every good solution
BadgesT1Good<-sqldf("SELECT
Name,
COUNT(*) AS Number,
MIN(Class) AS BestClass
FROM Badges
GROUP BY Name
ORDER BY Number DESC
LIMIT 10")
T2Good <- sqldf("SELECT Location, COUNT(*) AS Count
FROM (
SELECT Posts.OwnerUserId, Users.Id, Users.Location
FROM Users
JOIN Posts ON Users.Id = Posts.OwnerUserId
)
WHERE Location NOT IN ('')
GROUP BY Location
ORDER BY Count DESC
LIMIT 10")
T3Good <- sqldf("SELECT
Users.AccountId,
Users.DisplayName,
Users.Location,
AVG(PostAuth.AnswersCount) as AverageAnswersCount
FROM
(
SELECT
AnsCount.AnswersCount,
Posts.Id,
Posts.OwnerUserId
FROM (
SELECT Posts.ParentId, COUNT(*) AS AnswersCount
FROM Posts
WHERE Posts.PostTypeId = 2
GROUP BY Posts.ParentId
) AS AnsCount
JOIN Posts ON Posts.Id = AnsCount.ParentId
) AS PostAuth
JOIN Users ON Users.AccountId=PostAuth.OwnerUserId
GROUP BY OwnerUserId
ORDER BY AverageAnswersCount DESC
LIMIT 10")
T4Good <- sqldf("SELECT
Posts.Title,
UpVotesPerYear.Year,
MAX(UpVotesPerYear.Count) AS Count
FROM (
SELECT
PostId,
COUNT(*) AS Count,
STRFTIME('%Y', Votes.CreationDate) AS Year
FROM Votes
WHERE VoteTypeId=2
GROUP BY PostId, Year
) AS UpVotesPerYear
JOIN Posts ON Posts.Id=UpVotesPerYear.PostId
WHERE Posts.PostTypeId=1
GROUP BY Year
ORDER BY Year ASC")
T5Good<-sqldf("SELECT
Posts.Title,
VotesByAge2.OldVotes
FROM Posts
JOIN (
SELECT
PostId,
MAX(CASE WHEN VoteDate = 'new' THEN Total ELSE 0 END) NewVotes,
MAX(CASE WHEN VoteDate = 'old' THEN Total ELSE 0 END) OldVotes,
SUM(Total) AS Votes
FROM (
SELECT
PostId,
CASE STRFTIME('%Y', CreationDate)
WHEN '2021' THEN 'new'
WHEN '2020' THEN 'new'
ELSE 'old'
END VoteDate,
COUNT(*) AS Total
FROM Votes
WHERE VoteTypeId IN (1, 2, 5)
GROUP BY PostId, VoteDate
) AS VotesByAge
GROUP BY VotesByAge.PostId
HAVING NewVotes=0
) AS VotesByAge2 ON VotesByAge2.PostId=Posts.ID
WHERE Posts.PostTypeId=1
ORDER BY VotesByAge2.OldVotes DESC
LIMIT 10")
#===================================================================================================
# in the beginning of every task there is convertion of data frame to data table
badges <- data.table(Badges)
# grouping by Name, creating new columns that calculates rows and min
res<- badges[,list(Number=.N,BestClass=min(Class)),by="Name"]
# setting up order
res <- res[order(-Number),,]
# getting head of it
res <- head(res,10)
# we have to treat it as data frame to check if its good
check(as.data.frame(res)==BadgesT1Good)
#===================================================================================================
users <- data.table(Users)
posts <- data.table(Posts)
# we select spiecified fields from both tables
postsIds <- posts[,list(OwnerUserId),]
usersSmaller <- users[,list(Id,Location),]
# we join innerly by Id and OwnerUserId
innerTable2 <- usersSmaller[postsIds,on=list(Id==OwnerUserId),nomatch=0]
# filtering by location, grouping and counting rows, ordering and getting head
outerTable2<- innerTable2[Location!='',,]
outerTable2<-outerTable2[,list(Count=.N),by="Location"]
outerTable2<-outerTable2[order(-Count),,]
outerTable2<-head(outerTable2,10)
check(as.data.frame(outerTable2)==T2Good)
#===================================================================================================
# we get wanted fields, filter it, group by and getting number of rows
AnsCount <- posts[,list(ParentId,PostTypeId),]
AnsCount <- AnsCount[PostTypeId==2,,]
AnsCount <- AnsCount[,list(AnswerCount=.N),by="ParentId"]
# getting desired fields and joining
postsIdOwner <- posts[,list(Id,OwnerUserId),]
PostAuth<- postsIdOwner[AnsCount,on=list(Id==ParentId),nomatch=0]
# selecting fields
users3<-users[,list(AccountId,DisplayName,Location),]
# joining again
outerTable3 <- PostAuth[users3,on=list(OwnerUserId==AccountId),nomatch=0]
# grouping, ordering, getting head and rename
outerTable3 <- outerTable3[,list(AverageAnswersCount=mean(AnswerCount)),by=list(OwnerUserId,DisplayName,Location)]
outerTable3<- outerTable3[order(-AverageAnswersCount,-OwnerUserId),,]
outerTable3<-head(outerTable3,10)
setnames(outerTable3,"OwnerUserId","AccountId")
check(as.data.frame(outerTable3)==T3Good)
#===================================================================================================
votes<-data.table(Votes)
#creating additional column and getting columns we want, filtering, grouping and counting rows
UpVotesPerYear<-votes[,list(PostId,VoteTypeId,Year=strftime(CreationDate,format = '%Y')),]
UpVotesPerYear<-UpVotesPerYear[VoteTypeId==2,,]
UpVotesPerYear<-UpVotesPerYear[,list(Count=.N),by=list(PostId,Year)]
# seelcting columns and filtering rows
postsTitleId<-posts[PostTypeId==1,list(Title,Id),]
# joining tables
outerTable4 <- postsTitleId[UpVotesPerYear,on=list(Id==PostId),nomatch=0]
# selecting columns
postsHelper<-outerTable4[,list(Title,Year,Count),]
# grouping and getting max
outerTable4 <-postsHelper[,list(Count=max(Count)),by="Year"]
# ordering output
outerTable4 <- outerTable4[order(Year),,]
# joining tables again
outerTable4 <- outerTable4[postsHelper,on=list(Year,Count),nomatch=0]
#
outerTable4 <- outerTable4[,list(Title,Year,Count),]
check(as.data.frame(outerTable4)==T4Good)
#===================================================================================================
# filtering votes, creating new columns
VotesByAgeTable<- votes[VoteTypeId==1 | VoteTypeId==2 | VoteTypeId==5,,]
VotesByAgeTable<-VotesByAgeTable[,VoteDate := fcase(strftime(CreationDate,format = '%Y')=='2021','new',strftime(CreationDate,format = '%Y')=='2020','new',default = 'old'),]
# grouping by and counting rows
VotesByAgeTable <- VotesByAgeTable[,list(Total=.N),by=list(PostId,VoteDate)]
# grouping by and creating new columns with cases
VotesByAgeTable2 <- VotesByAgeTable[,list(NewVotes=max(fcase(VoteDate=='new',Total,default = as.integer(0))),OldVotes=max(fcase(VoteDate=='old',Total,default = as.integer(0))),Votes=sum(Total)),by=list(PostId)]
# filtering
VotesByAgeTable2 <- VotesByAgeTable2[NewVotes==0,,]
# filtering posts and getting columns we need
postsTask5 <- posts[PostTypeId==1,list(Title,Id),]
# joining tables
outerTable5 <- VotesByAgeTable2[postsTask5,on=list(PostId==Id),nomatch=0]
# ordering table and getting columns we want
outerTable5<-outerTable5[order(-OldVotes),list(Title,OldVotes),]
# getting 10 rows
outerTable5<-head(outerTable5,10)
check(as.data.frame(outerTable5)==T5Good)